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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008
  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 <https://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 <https://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. #ifdef __AVR__
  78. #include "stepper/speed_lookuptable.h"
  79. #endif
  80. #include "endstops.h"
  81. #include "planner.h"
  82. #include "motion.h"
  83. #include "../lcd/marlinui.h"
  84. #include "../gcode/queue.h"
  85. #include "../sd/cardreader.h"
  86. #include "../MarlinCore.h"
  87. #include "../HAL/shared/Delay.h"
  88. #if ENABLED(BD_SENSOR)
  89. #include "../feature/bedlevel/bdl/bdl.h"
  90. #endif
  91. #if ENABLED(INTEGRATED_BABYSTEPPING)
  92. #include "../feature/babystep.h"
  93. #endif
  94. #if MB(ALLIGATOR)
  95. #include "../feature/dac/dac_dac084s085.h"
  96. #endif
  97. #if HAS_MOTOR_CURRENT_SPI
  98. #include <SPI.h>
  99. #endif
  100. #if ENABLED(MIXING_EXTRUDER)
  101. #include "../feature/mixing.h"
  102. #endif
  103. #if HAS_FILAMENT_RUNOUT_DISTANCE
  104. #include "../feature/runout.h"
  105. #endif
  106. #if ENABLED(AUTO_POWER_CONTROL)
  107. #include "../feature/power.h"
  108. #endif
  109. #if ENABLED(POWER_LOSS_RECOVERY)
  110. #include "../feature/powerloss.h"
  111. #endif
  112. #if HAS_CUTTER
  113. #include "../feature/spindle_laser.h"
  114. #endif
  115. #if ENABLED(EXTENSIBLE_UI)
  116. #include "../lcd/extui/ui_api.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_SPI || HAS_MOTOR_CURRENT_PWM
  123. bool Stepper::initialized; // = false
  124. uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
  125. #if HAS_MOTOR_CURRENT_SPI
  126. constexpr uint32_t Stepper::digipot_count[];
  127. #endif
  128. #endif
  129. stepper_flags_t Stepper::axis_enabled; // {0}
  130. // private:
  131. block_t* Stepper::current_block; // (= nullptr) A pointer to the block currently being traced
  132. axis_bits_t Stepper::last_direction_bits, // = 0
  133. Stepper::axis_did_move; // = 0
  134. bool Stepper::abort_current_block;
  135. #if DISABLED(MIXING_EXTRUDER) && HAS_MULTI_EXTRUDER
  136. uint8_t Stepper::last_moved_extruder = 0xFF;
  137. #endif
  138. #if ENABLED(X_DUAL_ENDSTOPS)
  139. bool Stepper::locked_X_motor = false, Stepper::locked_X2_motor = false;
  140. #endif
  141. #if ENABLED(Y_DUAL_ENDSTOPS)
  142. bool Stepper::locked_Y_motor = false, Stepper::locked_Y2_motor = false;
  143. #endif
  144. #if EITHER(Z_MULTI_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  145. bool Stepper::locked_Z_motor = false, Stepper::locked_Z2_motor = false
  146. #if NUM_Z_STEPPERS >= 3
  147. , Stepper::locked_Z3_motor = false
  148. #if NUM_Z_STEPPERS >= 4
  149. , Stepper::locked_Z4_motor = false
  150. #endif
  151. #endif
  152. ;
  153. #endif
  154. uint32_t Stepper::acceleration_time, Stepper::deceleration_time;
  155. uint8_t Stepper::steps_per_isr;
  156. #if ENABLED(FREEZE_FEATURE)
  157. bool Stepper::frozen; // = false
  158. #endif
  159. IF_DISABLED(ADAPTIVE_STEP_SMOOTHING, constexpr) uint8_t Stepper::oversampling_factor;
  160. xyze_long_t Stepper::delta_error{0};
  161. xyze_ulong_t Stepper::advance_dividend{0};
  162. uint32_t Stepper::advance_divisor = 0,
  163. Stepper::step_events_completed = 0, // The number of step events executed in the current block
  164. Stepper::accelerate_until, // The count at which to stop accelerating
  165. Stepper::decelerate_after, // The count at which to start decelerating
  166. Stepper::step_event_count; // The total event count for the current block
  167. #if EITHER(HAS_MULTI_EXTRUDER, MIXING_EXTRUDER)
  168. uint8_t Stepper::stepper_extruder;
  169. #else
  170. constexpr uint8_t Stepper::stepper_extruder;
  171. #endif
  172. #if ENABLED(S_CURVE_ACCELERATION)
  173. int32_t __attribute__((used)) Stepper::bezier_A __asm__("bezier_A"); // A coefficient in Bézier speed curve with alias for assembler
  174. int32_t __attribute__((used)) Stepper::bezier_B __asm__("bezier_B"); // B coefficient in Bézier speed curve with alias for assembler
  175. int32_t __attribute__((used)) Stepper::bezier_C __asm__("bezier_C"); // C coefficient in Bézier speed curve with alias for assembler
  176. uint32_t __attribute__((used)) Stepper::bezier_F __asm__("bezier_F"); // F coefficient in Bézier speed curve with alias for assembler
  177. uint32_t __attribute__((used)) Stepper::bezier_AV __asm__("bezier_AV"); // AV coefficient in Bézier speed curve with alias for assembler
  178. #ifdef __AVR__
  179. bool __attribute__((used)) Stepper::A_negative __asm__("A_negative"); // If A coefficient was negative
  180. #endif
  181. bool Stepper::bezier_2nd_half; // =false If Bézier curve has been initialized or not
  182. #endif
  183. #if ENABLED(LIN_ADVANCE)
  184. uint32_t Stepper::nextAdvanceISR = LA_ADV_NEVER,
  185. Stepper::la_interval = LA_ADV_NEVER;
  186. int32_t Stepper::la_delta_error = 0,
  187. Stepper::la_dividend = 0,
  188. Stepper::la_advance_steps = 0;
  189. #endif
  190. #if ENABLED(INTEGRATED_BABYSTEPPING)
  191. uint32_t Stepper::nextBabystepISR = BABYSTEP_NEVER;
  192. #endif
  193. #if ENABLED(DIRECT_STEPPING)
  194. page_step_state_t Stepper::page_step_state;
  195. #endif
  196. int32_t Stepper::ticks_nominal = -1;
  197. #if DISABLED(S_CURVE_ACCELERATION)
  198. uint32_t Stepper::acc_step_rate; // needed for deceleration start point
  199. #endif
  200. xyz_long_t Stepper::endstops_trigsteps;
  201. xyze_long_t Stepper::count_position{0};
  202. xyze_int8_t Stepper::count_direction{0};
  203. #define MINDIR(A) (count_direction[_AXIS(A)] < 0)
  204. #define MAXDIR(A) (count_direction[_AXIS(A)] > 0)
  205. #define STEPTEST(A,M,I) TERN0(HAS_ ##A## ##I## _ ##M, !(TEST(endstops.state(), A## ##I## _ ##M) && M## DIR(A)) && !locked_ ##A## ##I## _motor)
  206. #define DUAL_ENDSTOP_APPLY_STEP(A,V) \
  207. if (separate_multi_axis) { \
  208. if (ENABLED(A##_HOME_TO_MIN)) { \
  209. if (STEPTEST(A,MIN, )) A## _STEP_WRITE(V); \
  210. if (STEPTEST(A,MIN,2)) A##2_STEP_WRITE(V); \
  211. } \
  212. else if (ENABLED(A##_HOME_TO_MAX)) { \
  213. if (STEPTEST(A,MAX, )) A## _STEP_WRITE(V); \
  214. if (STEPTEST(A,MAX,2)) A##2_STEP_WRITE(V); \
  215. } \
  216. } \
  217. else { \
  218. A##_STEP_WRITE(V); \
  219. A##2_STEP_WRITE(V); \
  220. }
  221. #define DUAL_SEPARATE_APPLY_STEP(A,V) \
  222. if (separate_multi_axis) { \
  223. if (!locked_##A## _motor) A## _STEP_WRITE(V); \
  224. if (!locked_##A##2_motor) A##2_STEP_WRITE(V); \
  225. } \
  226. else { \
  227. A##_STEP_WRITE(V); \
  228. A##2_STEP_WRITE(V); \
  229. }
  230. #define TRIPLE_ENDSTOP_APPLY_STEP(A,V) \
  231. if (separate_multi_axis) { \
  232. if (ENABLED(A##_HOME_TO_MIN)) { \
  233. if (STEPTEST(A,MIN, )) A## _STEP_WRITE(V); \
  234. if (STEPTEST(A,MIN,2)) A##2_STEP_WRITE(V); \
  235. if (STEPTEST(A,MIN,3)) A##3_STEP_WRITE(V); \
  236. } \
  237. else if (ENABLED(A##_HOME_TO_MAX)) { \
  238. if (STEPTEST(A,MAX, )) A## _STEP_WRITE(V); \
  239. if (STEPTEST(A,MAX,2)) A##2_STEP_WRITE(V); \
  240. if (STEPTEST(A,MAX,3)) A##3_STEP_WRITE(V); \
  241. } \
  242. } \
  243. else { \
  244. A##_STEP_WRITE(V); \
  245. A##2_STEP_WRITE(V); \
  246. A##3_STEP_WRITE(V); \
  247. }
  248. #define TRIPLE_SEPARATE_APPLY_STEP(A,V) \
  249. if (separate_multi_axis) { \
  250. if (!locked_##A## _motor) A## _STEP_WRITE(V); \
  251. if (!locked_##A##2_motor) A##2_STEP_WRITE(V); \
  252. if (!locked_##A##3_motor) A##3_STEP_WRITE(V); \
  253. } \
  254. else { \
  255. A## _STEP_WRITE(V); \
  256. A##2_STEP_WRITE(V); \
  257. A##3_STEP_WRITE(V); \
  258. }
  259. #define QUAD_ENDSTOP_APPLY_STEP(A,V) \
  260. if (separate_multi_axis) { \
  261. if (ENABLED(A##_HOME_TO_MIN)) { \
  262. if (STEPTEST(A,MIN, )) A## _STEP_WRITE(V); \
  263. if (STEPTEST(A,MIN,2)) A##2_STEP_WRITE(V); \
  264. if (STEPTEST(A,MIN,3)) A##3_STEP_WRITE(V); \
  265. if (STEPTEST(A,MIN,4)) A##4_STEP_WRITE(V); \
  266. } \
  267. else if (ENABLED(A##_HOME_TO_MAX)) { \
  268. if (STEPTEST(A,MAX, )) A## _STEP_WRITE(V); \
  269. if (STEPTEST(A,MAX,2)) A##2_STEP_WRITE(V); \
  270. if (STEPTEST(A,MAX,3)) A##3_STEP_WRITE(V); \
  271. if (STEPTEST(A,MAX,4)) A##4_STEP_WRITE(V); \
  272. } \
  273. } \
  274. else { \
  275. A## _STEP_WRITE(V); \
  276. A##2_STEP_WRITE(V); \
  277. A##3_STEP_WRITE(V); \
  278. A##4_STEP_WRITE(V); \
  279. }
  280. #define QUAD_SEPARATE_APPLY_STEP(A,V) \
  281. if (separate_multi_axis) { \
  282. if (!locked_##A## _motor) A## _STEP_WRITE(V); \
  283. if (!locked_##A##2_motor) A##2_STEP_WRITE(V); \
  284. if (!locked_##A##3_motor) A##3_STEP_WRITE(V); \
  285. if (!locked_##A##4_motor) A##4_STEP_WRITE(V); \
  286. } \
  287. else { \
  288. A## _STEP_WRITE(V); \
  289. A##2_STEP_WRITE(V); \
  290. A##3_STEP_WRITE(V); \
  291. A##4_STEP_WRITE(V); \
  292. }
  293. #if HAS_DUAL_X_STEPPERS
  294. #define X_APPLY_DIR(v,Q) do{ X_DIR_WRITE(v); X2_DIR_WRITE((v) ^ ENABLED(INVERT_X2_VS_X_DIR)); }while(0)
  295. #if ENABLED(X_DUAL_ENDSTOPS)
  296. #define X_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(X,v)
  297. #else
  298. #define X_APPLY_STEP(v,Q) do{ X_STEP_WRITE(v); X2_STEP_WRITE(v); }while(0)
  299. #endif
  300. #elif ENABLED(DUAL_X_CARRIAGE)
  301. #define X_APPLY_DIR(v,ALWAYS) do{ \
  302. if (extruder_duplication_enabled || ALWAYS) { X_DIR_WRITE(v); X2_DIR_WRITE((v) ^ idex_mirrored_mode); } \
  303. else if (last_moved_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  304. }while(0)
  305. #define X_APPLY_STEP(v,ALWAYS) do{ \
  306. if (extruder_duplication_enabled || ALWAYS) { X_STEP_WRITE(v); X2_STEP_WRITE(v); } \
  307. else if (last_moved_extruder) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  308. }while(0)
  309. #else
  310. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  311. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  312. #endif
  313. #if HAS_DUAL_Y_STEPPERS
  314. #define Y_APPLY_DIR(v,Q) do{ Y_DIR_WRITE(v); Y2_DIR_WRITE((v) ^ ENABLED(INVERT_Y2_VS_Y_DIR)); }while(0)
  315. #if ENABLED(Y_DUAL_ENDSTOPS)
  316. #define Y_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Y,v)
  317. #else
  318. #define Y_APPLY_STEP(v,Q) do{ Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }while(0)
  319. #endif
  320. #elif HAS_Y_AXIS
  321. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  322. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  323. #endif
  324. #if NUM_Z_STEPPERS == 4
  325. #define Z_APPLY_DIR(v,Q) do{ \
  326. Z_DIR_WRITE(v); Z2_DIR_WRITE((v) ^ ENABLED(INVERT_Z2_VS_Z_DIR)); \
  327. Z3_DIR_WRITE((v) ^ ENABLED(INVERT_Z3_VS_Z_DIR)); Z4_DIR_WRITE((v) ^ ENABLED(INVERT_Z4_VS_Z_DIR)); \
  328. }while(0)
  329. #if ENABLED(Z_MULTI_ENDSTOPS)
  330. #define Z_APPLY_STEP(v,Q) QUAD_ENDSTOP_APPLY_STEP(Z,v)
  331. #elif ENABLED(Z_STEPPER_AUTO_ALIGN)
  332. #define Z_APPLY_STEP(v,Q) QUAD_SEPARATE_APPLY_STEP(Z,v)
  333. #else
  334. #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)
  335. #endif
  336. #elif NUM_Z_STEPPERS == 3
  337. #define Z_APPLY_DIR(v,Q) do{ \
  338. Z_DIR_WRITE(v); Z2_DIR_WRITE((v) ^ ENABLED(INVERT_Z2_VS_Z_DIR)); Z3_DIR_WRITE((v) ^ ENABLED(INVERT_Z3_VS_Z_DIR)); \
  339. }while(0)
  340. #if ENABLED(Z_MULTI_ENDSTOPS)
  341. #define Z_APPLY_STEP(v,Q) TRIPLE_ENDSTOP_APPLY_STEP(Z,v)
  342. #elif ENABLED(Z_STEPPER_AUTO_ALIGN)
  343. #define Z_APPLY_STEP(v,Q) TRIPLE_SEPARATE_APPLY_STEP(Z,v)
  344. #else
  345. #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); Z3_STEP_WRITE(v); }while(0)
  346. #endif
  347. #elif NUM_Z_STEPPERS == 2
  348. #define Z_APPLY_DIR(v,Q) do{ Z_DIR_WRITE(v); Z2_DIR_WRITE((v) ^ ENABLED(INVERT_Z2_VS_Z_DIR)); }while(0)
  349. #if ENABLED(Z_MULTI_ENDSTOPS)
  350. #define Z_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Z,v)
  351. #elif ENABLED(Z_STEPPER_AUTO_ALIGN)
  352. #define Z_APPLY_STEP(v,Q) DUAL_SEPARATE_APPLY_STEP(Z,v)
  353. #else
  354. #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }while(0)
  355. #endif
  356. #elif HAS_Z_AXIS
  357. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  358. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  359. #endif
  360. #if HAS_I_AXIS
  361. #define I_APPLY_DIR(v,Q) I_DIR_WRITE(v)
  362. #define I_APPLY_STEP(v,Q) I_STEP_WRITE(v)
  363. #endif
  364. #if HAS_J_AXIS
  365. #define J_APPLY_DIR(v,Q) J_DIR_WRITE(v)
  366. #define J_APPLY_STEP(v,Q) J_STEP_WRITE(v)
  367. #endif
  368. #if HAS_K_AXIS
  369. #define K_APPLY_DIR(v,Q) K_DIR_WRITE(v)
  370. #define K_APPLY_STEP(v,Q) K_STEP_WRITE(v)
  371. #endif
  372. #if HAS_U_AXIS
  373. #define U_APPLY_DIR(v,Q) U_DIR_WRITE(v)
  374. #define U_APPLY_STEP(v,Q) U_STEP_WRITE(v)
  375. #endif
  376. #if HAS_V_AXIS
  377. #define V_APPLY_DIR(v,Q) V_DIR_WRITE(v)
  378. #define V_APPLY_STEP(v,Q) V_STEP_WRITE(v)
  379. #endif
  380. #if HAS_W_AXIS
  381. #define W_APPLY_DIR(v,Q) W_DIR_WRITE(v)
  382. #define W_APPLY_STEP(v,Q) W_STEP_WRITE(v)
  383. #endif
  384. #if DISABLED(MIXING_EXTRUDER)
  385. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(stepper_extruder, v)
  386. #endif
  387. #define CYCLES_TO_NS(CYC) (1000UL * (CYC) / ((F_CPU) / 1000000))
  388. #define NS_PER_PULSE_TIMER_TICK (1000000000UL / (STEPPER_TIMER_RATE))
  389. // Round up when converting from ns to timer ticks
  390. #define NS_TO_PULSE_TIMER_TICKS(NS) (((NS) + (NS_PER_PULSE_TIMER_TICK) / 2) / (NS_PER_PULSE_TIMER_TICK))
  391. #define TIMER_SETUP_NS (CYCLES_TO_NS(TIMER_READ_ADD_AND_STORE_CYCLES))
  392. #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)))
  393. #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)))
  394. #define USING_TIMED_PULSE() hal_timer_t start_pulse_count = 0
  395. #define START_TIMED_PULSE(DIR) (start_pulse_count = HAL_timer_get_count(MF_TIMER_PULSE))
  396. #define AWAIT_TIMED_PULSE(DIR) while (PULSE_##DIR##_TICK_COUNT > HAL_timer_get_count(MF_TIMER_PULSE) - start_pulse_count) { }
  397. #define START_HIGH_PULSE() START_TIMED_PULSE(HIGH)
  398. #define AWAIT_HIGH_PULSE() AWAIT_TIMED_PULSE(HIGH)
  399. #define START_LOW_PULSE() START_TIMED_PULSE(LOW)
  400. #define AWAIT_LOW_PULSE() AWAIT_TIMED_PULSE(LOW)
  401. #if MINIMUM_STEPPER_PRE_DIR_DELAY > 0
  402. #define DIR_WAIT_BEFORE() DELAY_NS(MINIMUM_STEPPER_PRE_DIR_DELAY)
  403. #else
  404. #define DIR_WAIT_BEFORE()
  405. #endif
  406. #if MINIMUM_STEPPER_POST_DIR_DELAY > 0
  407. #define DIR_WAIT_AFTER() DELAY_NS(MINIMUM_STEPPER_POST_DIR_DELAY)
  408. #else
  409. #define DIR_WAIT_AFTER()
  410. #endif
  411. void Stepper::enable_axis(const AxisEnum axis) {
  412. #define _CASE_ENABLE(N) case N##_AXIS: ENABLE_AXIS_##N(); break;
  413. switch (axis) {
  414. MAIN_AXIS_MAP(_CASE_ENABLE)
  415. default: break;
  416. }
  417. mark_axis_enabled(axis);
  418. }
  419. bool Stepper::disable_axis(const AxisEnum axis) {
  420. mark_axis_disabled(axis);
  421. TERN_(DWIN_LCD_PROUI, set_axis_untrusted(axis)); // MRISCOC workaround: https://github.com/MarlinFirmware/Marlin/issues/23095
  422. // If all the axes that share the enabled bit are disabled
  423. const bool can_disable = can_axis_disable(axis);
  424. if (can_disable) {
  425. #define _CASE_DISABLE(N) case N##_AXIS: DISABLE_AXIS_##N(); break;
  426. switch (axis) {
  427. MAIN_AXIS_MAP(_CASE_DISABLE)
  428. default: break;
  429. }
  430. }
  431. return can_disable;
  432. }
  433. #if HAS_EXTRUDERS
  434. void Stepper::enable_extruder(E_TERN_(const uint8_t eindex)) {
  435. IF_DISABLED(HAS_MULTI_EXTRUDER, constexpr uint8_t eindex = 0);
  436. #define _CASE_ENA_E(N) case N: ENABLE_AXIS_E##N(); mark_axis_enabled(E_AXIS E_OPTARG(eindex)); break;
  437. switch (eindex) {
  438. REPEAT(E_STEPPERS, _CASE_ENA_E)
  439. }
  440. }
  441. bool Stepper::disable_extruder(E_TERN_(const uint8_t eindex/*=0*/)) {
  442. IF_DISABLED(HAS_MULTI_EXTRUDER, constexpr uint8_t eindex = 0);
  443. mark_axis_disabled(E_AXIS E_OPTARG(eindex));
  444. const bool can_disable = can_axis_disable(E_AXIS E_OPTARG(eindex));
  445. if (can_disable) {
  446. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  447. switch (eindex) { REPEAT(E_STEPPERS, _CASE_DIS_E) }
  448. }
  449. return can_disable;
  450. }
  451. void Stepper::enable_e_steppers() {
  452. #define _ENA_E(N) ENABLE_EXTRUDER(N);
  453. REPEAT(EXTRUDERS, _ENA_E)
  454. }
  455. void Stepper::disable_e_steppers() {
  456. #define _DIS_E(N) DISABLE_EXTRUDER(N);
  457. REPEAT(EXTRUDERS, _DIS_E)
  458. }
  459. #endif
  460. void Stepper::enable_all_steppers() {
  461. TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
  462. NUM_AXIS_CODE(
  463. enable_axis(X_AXIS), enable_axis(Y_AXIS), enable_axis(Z_AXIS),
  464. enable_axis(I_AXIS), enable_axis(J_AXIS), enable_axis(K_AXIS),
  465. enable_axis(U_AXIS), enable_axis(V_AXIS), enable_axis(W_AXIS)
  466. );
  467. enable_e_steppers();
  468. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersEnabled());
  469. }
  470. void Stepper::disable_all_steppers() {
  471. NUM_AXIS_CODE(
  472. disable_axis(X_AXIS), disable_axis(Y_AXIS), disable_axis(Z_AXIS),
  473. disable_axis(I_AXIS), disable_axis(J_AXIS), disable_axis(K_AXIS),
  474. disable_axis(U_AXIS), disable_axis(V_AXIS), disable_axis(W_AXIS)
  475. );
  476. disable_e_steppers();
  477. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersDisabled());
  478. }
  479. /**
  480. * Set the stepper direction of each axis
  481. *
  482. * COREXY: X_AXIS=A_AXIS and Y_AXIS=B_AXIS
  483. * COREXZ: X_AXIS=A_AXIS and Z_AXIS=C_AXIS
  484. * COREYZ: Y_AXIS=B_AXIS and Z_AXIS=C_AXIS
  485. */
  486. void Stepper::set_directions() {
  487. DIR_WAIT_BEFORE();
  488. #define SET_STEP_DIR(A) \
  489. if (motor_direction(_AXIS(A))) { \
  490. A##_APPLY_DIR(INVERT_##A##_DIR, false); \
  491. count_direction[_AXIS(A)] = -1; \
  492. } \
  493. else { \
  494. A##_APPLY_DIR(!INVERT_##A##_DIR, false); \
  495. count_direction[_AXIS(A)] = 1; \
  496. }
  497. TERN_(HAS_X_DIR, SET_STEP_DIR(X)); // A
  498. TERN_(HAS_Y_DIR, SET_STEP_DIR(Y)); // B
  499. TERN_(HAS_Z_DIR, SET_STEP_DIR(Z)); // C
  500. TERN_(HAS_I_DIR, SET_STEP_DIR(I));
  501. TERN_(HAS_J_DIR, SET_STEP_DIR(J));
  502. TERN_(HAS_K_DIR, SET_STEP_DIR(K));
  503. TERN_(HAS_U_DIR, SET_STEP_DIR(U));
  504. TERN_(HAS_V_DIR, SET_STEP_DIR(V));
  505. TERN_(HAS_W_DIR, SET_STEP_DIR(W));
  506. #if ENABLED(MIXING_EXTRUDER)
  507. // Because this is valid for the whole block we don't know
  508. // what E steppers will step. Likely all. Set all.
  509. if (motor_direction(E_AXIS)) {
  510. MIXER_STEPPER_LOOP(j) REV_E_DIR(j);
  511. count_direction.e = -1;
  512. }
  513. else {
  514. MIXER_STEPPER_LOOP(j) NORM_E_DIR(j);
  515. count_direction.e = 1;
  516. }
  517. #elif HAS_EXTRUDERS
  518. if (motor_direction(E_AXIS)) {
  519. REV_E_DIR(stepper_extruder);
  520. count_direction.e = -1;
  521. }
  522. else {
  523. NORM_E_DIR(stepper_extruder);
  524. count_direction.e = 1;
  525. }
  526. #endif
  527. DIR_WAIT_AFTER();
  528. }
  529. #if ENABLED(S_CURVE_ACCELERATION)
  530. /**
  531. * This uses a quintic (fifth-degree) Bézier polynomial for the velocity curve, giving
  532. * a "linear pop" velocity curve; with pop being the sixth derivative of position:
  533. * velocity - 1st, acceleration - 2nd, jerk - 3rd, snap - 4th, crackle - 5th, pop - 6th
  534. *
  535. * The Bézier curve takes the form:
  536. *
  537. * 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)
  538. *
  539. * Where 0 <= t <= 1, and V(t) is the velocity. P_0 through P_5 are the control points, and B_0(t)
  540. * through B_5(t) are the Bernstein basis as follows:
  541. *
  542. * B_0(t) = (1-t)^5 = -t^5 + 5t^4 - 10t^3 + 10t^2 - 5t + 1
  543. * B_1(t) = 5(1-t)^4 * t = 5t^5 - 20t^4 + 30t^3 - 20t^2 + 5t
  544. * B_2(t) = 10(1-t)^3 * t^2 = -10t^5 + 30t^4 - 30t^3 + 10t^2
  545. * B_3(t) = 10(1-t)^2 * t^3 = 10t^5 - 20t^4 + 10t^3
  546. * B_4(t) = 5(1-t) * t^4 = -5t^5 + 5t^4
  547. * B_5(t) = t^5 = t^5
  548. * ^ ^ ^ ^ ^ ^
  549. * | | | | | |
  550. * A B C D E F
  551. *
  552. * Unfortunately, we cannot use forward-differencing to calculate each position through
  553. * the curve, as Marlin uses variable timer periods. So, we require a formula of the form:
  554. *
  555. * V_f(t) = A*t^5 + B*t^4 + C*t^3 + D*t^2 + E*t + F
  556. *
  557. * Looking at the above B_0(t) through B_5(t) expanded forms, if we take the coefficients of t^5
  558. * through t of the Bézier form of V(t), we can determine that:
  559. *
  560. * A = -P_0 + 5*P_1 - 10*P_2 + 10*P_3 - 5*P_4 + P_5
  561. * B = 5*P_0 - 20*P_1 + 30*P_2 - 20*P_3 + 5*P_4
  562. * C = -10*P_0 + 30*P_1 - 30*P_2 + 10*P_3
  563. * D = 10*P_0 - 20*P_1 + 10*P_2
  564. * E = - 5*P_0 + 5*P_1
  565. * F = P_0
  566. *
  567. * Now, since we will (currently) *always* want the initial acceleration and jerk values to be 0,
  568. * We set P_i = P_0 = P_1 = P_2 (initial velocity), and P_t = P_3 = P_4 = P_5 (target velocity),
  569. * which, after simplification, resolves to:
  570. *
  571. * A = - 6*P_i + 6*P_t = 6*(P_t - P_i)
  572. * B = 15*P_i - 15*P_t = 15*(P_i - P_t)
  573. * C = -10*P_i + 10*P_t = 10*(P_t - P_i)
  574. * D = 0
  575. * E = 0
  576. * F = P_i
  577. *
  578. * As the t is evaluated in non uniform steps here, there is no other way rather than evaluating
  579. * the Bézier curve at each point:
  580. *
  581. * V_f(t) = A*t^5 + B*t^4 + C*t^3 + F [0 <= t <= 1]
  582. *
  583. * Floating point arithmetic execution time cost is prohibitive, so we will transform the math to
  584. * use fixed point values to be able to evaluate it in realtime. Assuming a maximum of 250000 steps
  585. * per second (driver pulses should at least be 2µS hi/2µS lo), and allocating 2 bits to avoid
  586. * overflows on the evaluation of the Bézier curve, means we can use
  587. *
  588. * t: unsigned Q0.32 (0 <= t < 1) |range 0 to 0xFFFFFFFF unsigned
  589. * A: signed Q24.7 , |range = +/- 250000 * 6 * 128 = +/- 192000000 = 0x0B71B000 | 28 bits + sign
  590. * B: signed Q24.7 , |range = +/- 250000 *15 * 128 = +/- 480000000 = 0x1C9C3800 | 29 bits + sign
  591. * C: signed Q24.7 , |range = +/- 250000 *10 * 128 = +/- 320000000 = 0x1312D000 | 29 bits + sign
  592. * F: signed Q24.7 , |range = +/- 250000 * 128 = 32000000 = 0x01E84800 | 25 bits + sign
  593. *
  594. * The trapezoid generator state contains the following information, that we will use to create and evaluate
  595. * the Bézier curve:
  596. *
  597. * blk->step_event_count [TS] = The total count of steps for this movement. (=distance)
  598. * blk->initial_rate [VI] = The initial steps per second (=velocity)
  599. * blk->final_rate [VF] = The ending steps per second (=velocity)
  600. * and the count of events completed (step_events_completed) [CS] (=distance until now)
  601. *
  602. * Note the abbreviations we use in the following formulae are between []s
  603. *
  604. * For Any 32bit CPU:
  605. *
  606. * At the start of each trapezoid, calculate the coefficients A,B,C,F and Advance [AV], as follows:
  607. *
  608. * A = 6*128*(VF - VI) = 768*(VF - VI)
  609. * B = 15*128*(VI - VF) = 1920*(VI - VF)
  610. * C = 10*128*(VF - VI) = 1280*(VF - VI)
  611. * F = 128*VI = 128*VI
  612. * 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)
  613. *
  614. * And for each point, evaluate the curve with the following sequence:
  615. *
  616. * void lsrs(uint32_t& d, uint32_t s, int cnt) {
  617. * d = s >> cnt;
  618. * }
  619. * void lsls(uint32_t& d, uint32_t s, int cnt) {
  620. * d = s << cnt;
  621. * }
  622. * void lsrs(int32_t& d, uint32_t s, int cnt) {
  623. * d = uint32_t(s) >> cnt;
  624. * }
  625. * void lsls(int32_t& d, uint32_t s, int cnt) {
  626. * d = uint32_t(s) << cnt;
  627. * }
  628. * void umull(uint32_t& rlo, uint32_t& rhi, uint32_t op1, uint32_t op2) {
  629. * uint64_t res = uint64_t(op1) * op2;
  630. * rlo = uint32_t(res & 0xFFFFFFFF);
  631. * rhi = uint32_t((res >> 32) & 0xFFFFFFFF);
  632. * }
  633. * void smlal(int32_t& rlo, int32_t& rhi, int32_t op1, int32_t op2) {
  634. * int64_t mul = int64_t(op1) * op2;
  635. * int64_t s = int64_t(uint32_t(rlo) | ((uint64_t(uint32_t(rhi)) << 32U)));
  636. * mul += s;
  637. * rlo = int32_t(mul & 0xFFFFFFFF);
  638. * rhi = int32_t((mul >> 32) & 0xFFFFFFFF);
  639. * }
  640. * int32_t _eval_bezier_curve_arm(uint32_t curr_step) {
  641. * uint32_t flo = 0;
  642. * uint32_t fhi = bezier_AV * curr_step;
  643. * uint32_t t = fhi;
  644. * int32_t alo = bezier_F;
  645. * int32_t ahi = 0;
  646. * int32_t A = bezier_A;
  647. * int32_t B = bezier_B;
  648. * int32_t C = bezier_C;
  649. *
  650. * lsrs(ahi, alo, 1); // a = F << 31
  651. * lsls(alo, alo, 31); //
  652. * umull(flo, fhi, fhi, t); // f *= t
  653. * umull(flo, fhi, fhi, t); // f>>=32; f*=t
  654. * lsrs(flo, fhi, 1); //
  655. * smlal(alo, ahi, flo, C); // a+=(f>>33)*C
  656. * umull(flo, fhi, fhi, t); // f>>=32; f*=t
  657. * lsrs(flo, fhi, 1); //
  658. * smlal(alo, ahi, flo, B); // a+=(f>>33)*B
  659. * umull(flo, fhi, fhi, t); // f>>=32; f*=t
  660. * lsrs(flo, fhi, 1); // f>>=33;
  661. * smlal(alo, ahi, flo, A); // a+=(f>>33)*A;
  662. * lsrs(alo, ahi, 6); // a>>=38
  663. *
  664. * return alo;
  665. * }
  666. *
  667. * This is rewritten in ARM assembly for optimal performance (43 cycles to execute).
  668. *
  669. * For AVR, the precision of coefficients is scaled so the Bézier curve can be evaluated in real-time:
  670. * Let's reduce precision as much as possible. After some experimentation we found that:
  671. *
  672. * Assume t and AV with 24 bits is enough
  673. * A = 6*(VF - VI)
  674. * B = 15*(VI - VF)
  675. * C = 10*(VF - VI)
  676. * F = VI
  677. * AV = (1<<24)/TS (this is computed at the planner, to offload expensive calculations from the ISR)
  678. *
  679. * Instead of storing sign for each coefficient, we will store its absolute value,
  680. * and flag the sign of the A coefficient, so we can save to store the sign bit.
  681. * It always holds that sign(A) = - sign(B) = sign(C)
  682. *
  683. * So, the resulting range of the coefficients are:
  684. *
  685. * t: unsigned (0 <= t < 1) |range 0 to 0xFFFFFF unsigned
  686. * A: signed Q24 , range = 250000 * 6 = 1500000 = 0x16E360 | 21 bits
  687. * B: signed Q24 , range = 250000 *15 = 3750000 = 0x393870 | 22 bits
  688. * C: signed Q24 , range = 250000 *10 = 2500000 = 0x1312D0 | 21 bits
  689. * F: signed Q24 , range = 250000 = 250000 = 0x0ED090 | 20 bits
  690. *
  691. * And for each curve, estimate its coefficients with:
  692. *
  693. * void _calc_bezier_curve_coeffs(int32_t v0, int32_t v1, uint32_t av) {
  694. * // Calculate the Bézier coefficients
  695. * if (v1 < v0) {
  696. * A_negative = true;
  697. * bezier_A = 6 * (v0 - v1);
  698. * bezier_B = 15 * (v0 - v1);
  699. * bezier_C = 10 * (v0 - v1);
  700. * }
  701. * else {
  702. * A_negative = false;
  703. * bezier_A = 6 * (v1 - v0);
  704. * bezier_B = 15 * (v1 - v0);
  705. * bezier_C = 10 * (v1 - v0);
  706. * }
  707. * bezier_F = v0;
  708. * }
  709. *
  710. * And for each point, evaluate the curve with the following sequence:
  711. *
  712. * // unsigned multiplication of 24 bits x 24bits, return upper 16 bits
  713. * void umul24x24to16hi(uint16_t& r, uint24_t op1, uint24_t op2) {
  714. * r = (uint64_t(op1) * op2) >> 8;
  715. * }
  716. * // unsigned multiplication of 16 bits x 16bits, return upper 16 bits
  717. * void umul16x16to16hi(uint16_t& r, uint16_t op1, uint16_t op2) {
  718. * r = (uint32_t(op1) * op2) >> 16;
  719. * }
  720. * // unsigned multiplication of 16 bits x 24bits, return upper 24 bits
  721. * void umul16x24to24hi(uint24_t& r, uint16_t op1, uint24_t op2) {
  722. * r = uint24_t((uint64_t(op1) * op2) >> 16);
  723. * }
  724. *
  725. * int32_t _eval_bezier_curve(uint32_t curr_step) {
  726. * // To save computing, the first step is always the initial speed
  727. * if (!curr_step)
  728. * return bezier_F;
  729. *
  730. * uint16_t t;
  731. * umul24x24to16hi(t, bezier_AV, curr_step); // t: Range 0 - 1^16 = 16 bits
  732. * uint16_t f = t;
  733. * umul16x16to16hi(f, f, t); // Range 16 bits (unsigned)
  734. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^3 (unsigned)
  735. * uint24_t acc = bezier_F; // Range 20 bits (unsigned)
  736. * if (A_negative) {
  737. * uint24_t v;
  738. * umul16x24to24hi(v, f, bezier_C); // Range 21bits
  739. * acc -= v;
  740. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^4 (unsigned)
  741. * umul16x24to24hi(v, f, bezier_B); // Range 22bits
  742. * acc += v;
  743. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^5 (unsigned)
  744. * umul16x24to24hi(v, f, bezier_A); // Range 21bits + 15 = 36bits (plus sign)
  745. * acc -= v;
  746. * }
  747. * else {
  748. * uint24_t v;
  749. * umul16x24to24hi(v, f, bezier_C); // Range 21bits
  750. * acc += v;
  751. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^4 (unsigned)
  752. * umul16x24to24hi(v, f, bezier_B); // Range 22bits
  753. * acc -= v;
  754. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^5 (unsigned)
  755. * umul16x24to24hi(v, f, bezier_A); // Range 21bits + 15 = 36bits (plus sign)
  756. * acc += v;
  757. * }
  758. * return acc;
  759. * }
  760. * These functions are translated to assembler for optimal performance.
  761. * Coefficient calculation takes 70 cycles. Bezier point evaluation takes 150 cycles.
  762. */
  763. #ifdef __AVR__
  764. // For AVR we use assembly to maximize speed
  765. void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av) {
  766. // Store advance
  767. bezier_AV = av;
  768. // Calculate the rest of the coefficients
  769. uint8_t r2 = v0 & 0xFF;
  770. uint8_t r3 = (v0 >> 8) & 0xFF;
  771. uint8_t r12 = (v0 >> 16) & 0xFF;
  772. uint8_t r5 = v1 & 0xFF;
  773. uint8_t r6 = (v1 >> 8) & 0xFF;
  774. uint8_t r7 = (v1 >> 16) & 0xFF;
  775. uint8_t r4,r8,r9,r10,r11;
  776. __asm__ __volatile__(
  777. /* Calculate the Bézier coefficients */
  778. /* %10:%1:%0 = v0*/
  779. /* %5:%4:%3 = v1*/
  780. /* %7:%6:%10 = temporary*/
  781. /* %9 = val (must be high register!)*/
  782. /* %10 (must be high register!)*/
  783. /* Store initial velocity*/
  784. A("sts bezier_F, %0")
  785. A("sts bezier_F+1, %1")
  786. A("sts bezier_F+2, %10") /* bezier_F = %10:%1:%0 = v0 */
  787. /* Get delta speed */
  788. A("ldi %2,-1") /* %2 = 0xFF, means A_negative = true */
  789. A("clr %8") /* %8 = 0 */
  790. A("sub %0,%3")
  791. A("sbc %1,%4")
  792. A("sbc %10,%5") /* v0 -= v1, C=1 if result is negative */
  793. A("brcc 1f") /* branch if result is positive (C=0), that means v0 >= v1 */
  794. /* Result was negative, get the absolute value*/
  795. A("com %10")
  796. A("com %1")
  797. A("neg %0")
  798. A("sbc %1,%2")
  799. A("sbc %10,%2") /* %10:%1:%0 +1 -> %10:%1:%0 = -(v0 - v1) = (v1 - v0) */
  800. A("clr %2") /* %2 = 0, means A_negative = false */
  801. /* Store negative flag*/
  802. L("1")
  803. A("sts A_negative, %2") /* Store negative flag */
  804. /* Compute coefficients A,B and C [20 cycles worst case]*/
  805. A("ldi %9,6") /* %9 = 6 */
  806. A("mul %0,%9") /* r1:r0 = 6*LO(v0-v1) */
  807. A("sts bezier_A, r0")
  808. A("mov %6,r1")
  809. A("clr %7") /* %7:%6:r0 = 6*LO(v0-v1) */
  810. A("mul %1,%9") /* r1:r0 = 6*MI(v0-v1) */
  811. A("add %6,r0")
  812. A("adc %7,r1") /* %7:%6:?? += 6*MI(v0-v1) << 8 */
  813. A("mul %10,%9") /* r1:r0 = 6*HI(v0-v1) */
  814. A("add %7,r0") /* %7:%6:?? += 6*HI(v0-v1) << 16 */
  815. A("sts bezier_A+1, %6")
  816. A("sts bezier_A+2, %7") /* bezier_A = %7:%6:?? = 6*(v0-v1) [35 cycles worst] */
  817. A("ldi %9,15") /* %9 = 15 */
  818. A("mul %0,%9") /* r1:r0 = 5*LO(v0-v1) */
  819. A("sts bezier_B, r0")
  820. A("mov %6,r1")
  821. A("clr %7") /* %7:%6:?? = 5*LO(v0-v1) */
  822. A("mul %1,%9") /* r1:r0 = 5*MI(v0-v1) */
  823. A("add %6,r0")
  824. A("adc %7,r1") /* %7:%6:?? += 5*MI(v0-v1) << 8 */
  825. A("mul %10,%9") /* r1:r0 = 5*HI(v0-v1) */
  826. A("add %7,r0") /* %7:%6:?? += 5*HI(v0-v1) << 16 */
  827. A("sts bezier_B+1, %6")
  828. A("sts bezier_B+2, %7") /* bezier_B = %7:%6:?? = 5*(v0-v1) [50 cycles worst] */
  829. A("ldi %9,10") /* %9 = 10 */
  830. A("mul %0,%9") /* r1:r0 = 10*LO(v0-v1) */
  831. A("sts bezier_C, r0")
  832. A("mov %6,r1")
  833. A("clr %7") /* %7:%6:?? = 10*LO(v0-v1) */
  834. A("mul %1,%9") /* r1:r0 = 10*MI(v0-v1) */
  835. A("add %6,r0")
  836. A("adc %7,r1") /* %7:%6:?? += 10*MI(v0-v1) << 8 */
  837. A("mul %10,%9") /* r1:r0 = 10*HI(v0-v1) */
  838. A("add %7,r0") /* %7:%6:?? += 10*HI(v0-v1) << 16 */
  839. A("sts bezier_C+1, %6")
  840. " sts bezier_C+2, %7" /* bezier_C = %7:%6:?? = 10*(v0-v1) [65 cycles worst] */
  841. : "+r" (r2),
  842. "+d" (r3),
  843. "=r" (r4),
  844. "+r" (r5),
  845. "+r" (r6),
  846. "+r" (r7),
  847. "=r" (r8),
  848. "=r" (r9),
  849. "=r" (r10),
  850. "=d" (r11),
  851. "+r" (r12)
  852. :
  853. : "r0", "r1", "cc", "memory"
  854. );
  855. }
  856. FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
  857. // If dealing with the first step, save expensive computing and return the initial speed
  858. if (!curr_step)
  859. return bezier_F;
  860. uint8_t r0 = 0; /* Zero register */
  861. uint8_t r2 = (curr_step) & 0xFF;
  862. uint8_t r3 = (curr_step >> 8) & 0xFF;
  863. uint8_t r4 = (curr_step >> 16) & 0xFF;
  864. uint8_t r1,r5,r6,r7,r8,r9,r10,r11; /* Temporary registers */
  865. __asm__ __volatile(
  866. /* umul24x24to16hi(t, bezier_AV, curr_step); t: Range 0 - 1^16 = 16 bits*/
  867. A("lds %9,bezier_AV") /* %9 = LO(AV)*/
  868. A("mul %9,%2") /* r1:r0 = LO(bezier_AV)*LO(curr_step)*/
  869. A("mov %7,r1") /* %7 = LO(bezier_AV)*LO(curr_step) >> 8*/
  870. A("clr %8") /* %8:%7 = LO(bezier_AV)*LO(curr_step) >> 8*/
  871. A("lds %10,bezier_AV+1") /* %10 = MI(AV)*/
  872. A("mul %10,%2") /* r1:r0 = MI(bezier_AV)*LO(curr_step)*/
  873. A("add %7,r0")
  874. A("adc %8,r1") /* %8:%7 += MI(bezier_AV)*LO(curr_step)*/
  875. A("lds r1,bezier_AV+2") /* r11 = HI(AV)*/
  876. A("mul r1,%2") /* r1:r0 = HI(bezier_AV)*LO(curr_step)*/
  877. A("add %8,r0") /* %8:%7 += HI(bezier_AV)*LO(curr_step) << 8*/
  878. A("mul %9,%3") /* r1:r0 = LO(bezier_AV)*MI(curr_step)*/
  879. A("add %7,r0")
  880. A("adc %8,r1") /* %8:%7 += LO(bezier_AV)*MI(curr_step)*/
  881. A("mul %10,%3") /* r1:r0 = MI(bezier_AV)*MI(curr_step)*/
  882. A("add %8,r0") /* %8:%7 += LO(bezier_AV)*MI(curr_step) << 8*/
  883. A("mul %9,%4") /* r1:r0 = LO(bezier_AV)*HI(curr_step)*/
  884. A("add %8,r0") /* %8:%7 += LO(bezier_AV)*HI(curr_step) << 8*/
  885. /* %8:%7 = t*/
  886. /* uint16_t f = t;*/
  887. A("mov %5,%7") /* %6:%5 = f*/
  888. A("mov %6,%8")
  889. /* %6:%5 = f*/
  890. /* umul16x16to16hi(f, f, t); / Range 16 bits (unsigned) [17] */
  891. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  892. A("mov %9,r1") /* store MIL(LO(f) * LO(t)) in %9, we need it for rounding*/
  893. A("clr %10") /* %10 = 0*/
  894. A("clr %11") /* %11 = 0*/
  895. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  896. A("add %9,r0") /* %9 += LO(LO(f) * HI(t))*/
  897. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  898. A("adc %11,%0") /* %11 += carry*/
  899. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  900. A("add %9,r0") /* %9 += LO(HI(f) * LO(t))*/
  901. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t)) */
  902. A("adc %11,%0") /* %11 += carry*/
  903. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  904. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  905. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  906. A("mov %5,%10") /* %6:%5 = */
  907. A("mov %6,%11") /* f = %10:%11*/
  908. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3 (unsigned) [17]*/
  909. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  910. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  911. A("clr %10") /* %10 = 0*/
  912. A("clr %11") /* %11 = 0*/
  913. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  914. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  915. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  916. A("adc %11,%0") /* %11 += carry*/
  917. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  918. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  919. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  920. A("adc %11,%0") /* %11 += carry*/
  921. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  922. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  923. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  924. A("mov %5,%10") /* %6:%5 =*/
  925. A("mov %6,%11") /* f = %10:%11*/
  926. /* [15 +17*2] = [49]*/
  927. /* %4:%3:%2 will be acc from now on*/
  928. /* uint24_t acc = bezier_F; / Range 20 bits (unsigned)*/
  929. A("clr %9") /* "decimal place we get for free"*/
  930. A("lds %2,bezier_F")
  931. A("lds %3,bezier_F+1")
  932. A("lds %4,bezier_F+2") /* %4:%3:%2 = acc*/
  933. /* if (A_negative) {*/
  934. A("lds r0,A_negative")
  935. A("or r0,%0") /* Is flag signalling negative? */
  936. A("brne 3f") /* If yes, Skip next instruction if A was negative*/
  937. A("rjmp 1f") /* Otherwise, jump */
  938. /* uint24_t v; */
  939. /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29] */
  940. /* acc -= v; */
  941. L("3")
  942. A("lds %10, bezier_C") /* %10 = LO(bezier_C)*/
  943. A("mul %10,%5") /* r1:r0 = LO(bezier_C) * LO(f)*/
  944. A("sub %9,r1")
  945. A("sbc %2,%0")
  946. A("sbc %3,%0")
  947. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(LO(bezier_C) * LO(f))*/
  948. A("lds %11, bezier_C+1") /* %11 = MI(bezier_C)*/
  949. A("mul %11,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  950. A("sub %9,r0")
  951. A("sbc %2,r1")
  952. A("sbc %3,%0")
  953. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_C) * LO(f)*/
  954. A("lds %1, bezier_C+2") /* %1 = HI(bezier_C)*/
  955. A("mul %1,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  956. A("sub %2,r0")
  957. A("sbc %3,r1")
  958. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 8*/
  959. A("mul %10,%6") /* r1:r0 = LO(bezier_C) * MI(f)*/
  960. A("sub %9,r0")
  961. A("sbc %2,r1")
  962. A("sbc %3,%0")
  963. A("sbc %4,%0") /* %4:%3:%2:%9 -= LO(bezier_C) * MI(f)*/
  964. A("mul %11,%6") /* r1:r0 = MI(bezier_C) * MI(f)*/
  965. A("sub %2,r0")
  966. A("sbc %3,r1")
  967. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_C) * MI(f) << 8*/
  968. A("mul %1,%6") /* r1:r0 = HI(bezier_C) * LO(f)*/
  969. A("sub %3,r0")
  970. A("sbc %4,r1") /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 16*/
  971. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3 (unsigned) [17]*/
  972. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  973. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  974. A("clr %10") /* %10 = 0*/
  975. A("clr %11") /* %11 = 0*/
  976. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  977. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  978. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  979. A("adc %11,%0") /* %11 += carry*/
  980. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  981. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  982. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  983. A("adc %11,%0") /* %11 += carry*/
  984. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  985. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  986. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  987. A("mov %5,%10") /* %6:%5 =*/
  988. A("mov %6,%11") /* f = %10:%11*/
  989. /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
  990. /* acc += v; */
  991. A("lds %10, bezier_B") /* %10 = LO(bezier_B)*/
  992. A("mul %10,%5") /* r1:r0 = LO(bezier_B) * LO(f)*/
  993. A("add %9,r1")
  994. A("adc %2,%0")
  995. A("adc %3,%0")
  996. A("adc %4,%0") /* %4:%3:%2:%9 += HI(LO(bezier_B) * LO(f))*/
  997. A("lds %11, bezier_B+1") /* %11 = MI(bezier_B)*/
  998. A("mul %11,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  999. A("add %9,r0")
  1000. A("adc %2,r1")
  1001. A("adc %3,%0")
  1002. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_B) * LO(f)*/
  1003. A("lds %1, bezier_B+2") /* %1 = HI(bezier_B)*/
  1004. A("mul %1,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  1005. A("add %2,r0")
  1006. A("adc %3,r1")
  1007. A("adc %4,%0") /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 8*/
  1008. A("mul %10,%6") /* r1:r0 = LO(bezier_B) * MI(f)*/
  1009. A("add %9,r0")
  1010. A("adc %2,r1")
  1011. A("adc %3,%0")
  1012. A("adc %4,%0") /* %4:%3:%2:%9 += LO(bezier_B) * MI(f)*/
  1013. A("mul %11,%6") /* r1:r0 = MI(bezier_B) * MI(f)*/
  1014. A("add %2,r0")
  1015. A("adc %3,r1")
  1016. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_B) * MI(f) << 8*/
  1017. A("mul %1,%6") /* r1:r0 = HI(bezier_B) * LO(f)*/
  1018. A("add %3,r0")
  1019. A("adc %4,r1") /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 16*/
  1020. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5 (unsigned) [17]*/
  1021. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  1022. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  1023. A("clr %10") /* %10 = 0*/
  1024. A("clr %11") /* %11 = 0*/
  1025. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  1026. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  1027. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  1028. A("adc %11,%0") /* %11 += carry*/
  1029. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  1030. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  1031. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  1032. A("adc %11,%0") /* %11 += carry*/
  1033. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  1034. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  1035. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  1036. A("mov %5,%10") /* %6:%5 =*/
  1037. A("mov %6,%11") /* f = %10:%11*/
  1038. /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
  1039. /* acc -= v; */
  1040. A("lds %10, bezier_A") /* %10 = LO(bezier_A)*/
  1041. A("mul %10,%5") /* r1:r0 = LO(bezier_A) * LO(f)*/
  1042. A("sub %9,r1")
  1043. A("sbc %2,%0")
  1044. A("sbc %3,%0")
  1045. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(LO(bezier_A) * LO(f))*/
  1046. A("lds %11, bezier_A+1") /* %11 = MI(bezier_A)*/
  1047. A("mul %11,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  1048. A("sub %9,r0")
  1049. A("sbc %2,r1")
  1050. A("sbc %3,%0")
  1051. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_A) * LO(f)*/
  1052. A("lds %1, bezier_A+2") /* %1 = HI(bezier_A)*/
  1053. A("mul %1,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  1054. A("sub %2,r0")
  1055. A("sbc %3,r1")
  1056. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 8*/
  1057. A("mul %10,%6") /* r1:r0 = LO(bezier_A) * MI(f)*/
  1058. A("sub %9,r0")
  1059. A("sbc %2,r1")
  1060. A("sbc %3,%0")
  1061. A("sbc %4,%0") /* %4:%3:%2:%9 -= LO(bezier_A) * MI(f)*/
  1062. A("mul %11,%6") /* r1:r0 = MI(bezier_A) * MI(f)*/
  1063. A("sub %2,r0")
  1064. A("sbc %3,r1")
  1065. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_A) * MI(f) << 8*/
  1066. A("mul %1,%6") /* r1:r0 = HI(bezier_A) * LO(f)*/
  1067. A("sub %3,r0")
  1068. A("sbc %4,r1") /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 16*/
  1069. A("jmp 2f") /* Done!*/
  1070. L("1")
  1071. /* uint24_t v; */
  1072. /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29]*/
  1073. /* acc += v; */
  1074. A("lds %10, bezier_C") /* %10 = LO(bezier_C)*/
  1075. A("mul %10,%5") /* r1:r0 = LO(bezier_C) * LO(f)*/
  1076. A("add %9,r1")
  1077. A("adc %2,%0")
  1078. A("adc %3,%0")
  1079. A("adc %4,%0") /* %4:%3:%2:%9 += HI(LO(bezier_C) * LO(f))*/
  1080. A("lds %11, bezier_C+1") /* %11 = MI(bezier_C)*/
  1081. A("mul %11,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  1082. A("add %9,r0")
  1083. A("adc %2,r1")
  1084. A("adc %3,%0")
  1085. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_C) * LO(f)*/
  1086. A("lds %1, bezier_C+2") /* %1 = HI(bezier_C)*/
  1087. A("mul %1,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  1088. A("add %2,r0")
  1089. A("adc %3,r1")
  1090. A("adc %4,%0") /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 8*/
  1091. A("mul %10,%6") /* r1:r0 = LO(bezier_C) * MI(f)*/
  1092. A("add %9,r0")
  1093. A("adc %2,r1")
  1094. A("adc %3,%0")
  1095. A("adc %4,%0") /* %4:%3:%2:%9 += LO(bezier_C) * MI(f)*/
  1096. A("mul %11,%6") /* r1:r0 = MI(bezier_C) * MI(f)*/
  1097. A("add %2,r0")
  1098. A("adc %3,r1")
  1099. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_C) * MI(f) << 8*/
  1100. A("mul %1,%6") /* r1:r0 = HI(bezier_C) * LO(f)*/
  1101. A("add %3,r0")
  1102. A("adc %4,r1") /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 16*/
  1103. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3 (unsigned) [17]*/
  1104. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  1105. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  1106. A("clr %10") /* %10 = 0*/
  1107. A("clr %11") /* %11 = 0*/
  1108. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  1109. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  1110. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  1111. A("adc %11,%0") /* %11 += carry*/
  1112. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  1113. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  1114. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  1115. A("adc %11,%0") /* %11 += carry*/
  1116. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  1117. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  1118. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  1119. A("mov %5,%10") /* %6:%5 =*/
  1120. A("mov %6,%11") /* f = %10:%11*/
  1121. /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
  1122. /* acc -= v;*/
  1123. A("lds %10, bezier_B") /* %10 = LO(bezier_B)*/
  1124. A("mul %10,%5") /* r1:r0 = LO(bezier_B) * LO(f)*/
  1125. A("sub %9,r1")
  1126. A("sbc %2,%0")
  1127. A("sbc %3,%0")
  1128. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(LO(bezier_B) * LO(f))*/
  1129. A("lds %11, bezier_B+1") /* %11 = MI(bezier_B)*/
  1130. A("mul %11,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  1131. A("sub %9,r0")
  1132. A("sbc %2,r1")
  1133. A("sbc %3,%0")
  1134. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_B) * LO(f)*/
  1135. A("lds %1, bezier_B+2") /* %1 = HI(bezier_B)*/
  1136. A("mul %1,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  1137. A("sub %2,r0")
  1138. A("sbc %3,r1")
  1139. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 8*/
  1140. A("mul %10,%6") /* r1:r0 = LO(bezier_B) * MI(f)*/
  1141. A("sub %9,r0")
  1142. A("sbc %2,r1")
  1143. A("sbc %3,%0")
  1144. A("sbc %4,%0") /* %4:%3:%2:%9 -= LO(bezier_B) * MI(f)*/
  1145. A("mul %11,%6") /* r1:r0 = MI(bezier_B) * MI(f)*/
  1146. A("sub %2,r0")
  1147. A("sbc %3,r1")
  1148. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_B) * MI(f) << 8*/
  1149. A("mul %1,%6") /* r1:r0 = HI(bezier_B) * LO(f)*/
  1150. A("sub %3,r0")
  1151. A("sbc %4,r1") /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 16*/
  1152. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5 (unsigned) [17]*/
  1153. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  1154. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  1155. A("clr %10") /* %10 = 0*/
  1156. A("clr %11") /* %11 = 0*/
  1157. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  1158. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  1159. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  1160. A("adc %11,%0") /* %11 += carry*/
  1161. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  1162. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  1163. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  1164. A("adc %11,%0") /* %11 += carry*/
  1165. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  1166. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  1167. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  1168. A("mov %5,%10") /* %6:%5 =*/
  1169. A("mov %6,%11") /* f = %10:%11*/
  1170. /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
  1171. /* acc += v; */
  1172. A("lds %10, bezier_A") /* %10 = LO(bezier_A)*/
  1173. A("mul %10,%5") /* r1:r0 = LO(bezier_A) * LO(f)*/
  1174. A("add %9,r1")
  1175. A("adc %2,%0")
  1176. A("adc %3,%0")
  1177. A("adc %4,%0") /* %4:%3:%2:%9 += HI(LO(bezier_A) * LO(f))*/
  1178. A("lds %11, bezier_A+1") /* %11 = MI(bezier_A)*/
  1179. A("mul %11,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  1180. A("add %9,r0")
  1181. A("adc %2,r1")
  1182. A("adc %3,%0")
  1183. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_A) * LO(f)*/
  1184. A("lds %1, bezier_A+2") /* %1 = HI(bezier_A)*/
  1185. A("mul %1,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  1186. A("add %2,r0")
  1187. A("adc %3,r1")
  1188. A("adc %4,%0") /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 8*/
  1189. A("mul %10,%6") /* r1:r0 = LO(bezier_A) * MI(f)*/
  1190. A("add %9,r0")
  1191. A("adc %2,r1")
  1192. A("adc %3,%0")
  1193. A("adc %4,%0") /* %4:%3:%2:%9 += LO(bezier_A) * MI(f)*/
  1194. A("mul %11,%6") /* r1:r0 = MI(bezier_A) * MI(f)*/
  1195. A("add %2,r0")
  1196. A("adc %3,r1")
  1197. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_A) * MI(f) << 8*/
  1198. A("mul %1,%6") /* r1:r0 = HI(bezier_A) * LO(f)*/
  1199. A("add %3,r0")
  1200. A("adc %4,r1") /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 16*/
  1201. L("2")
  1202. " clr __zero_reg__" /* C runtime expects r1 = __zero_reg__ = 0 */
  1203. : "+r"(r0),
  1204. "+r"(r1),
  1205. "+r"(r2),
  1206. "+r"(r3),
  1207. "+r"(r4),
  1208. "+r"(r5),
  1209. "+r"(r6),
  1210. "+r"(r7),
  1211. "+r"(r8),
  1212. "+r"(r9),
  1213. "+r"(r10),
  1214. "+r"(r11)
  1215. :
  1216. :"cc","r0","r1"
  1217. );
  1218. return (r2 | (uint16_t(r3) << 8)) | (uint32_t(r4) << 16);
  1219. }
  1220. #else
  1221. // For all the other 32bit CPUs
  1222. FORCE_INLINE void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av) {
  1223. // Calculate the Bézier coefficients
  1224. bezier_A = 768 * (v1 - v0);
  1225. bezier_B = 1920 * (v0 - v1);
  1226. bezier_C = 1280 * (v1 - v0);
  1227. bezier_F = 128 * v0;
  1228. bezier_AV = av;
  1229. }
  1230. FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
  1231. #if (defined(__arm__) || defined(__thumb__)) && __ARM_ARCH >= 6 && !defined(STM32G0B1xx) // TODO: Test define STM32G0xx versus STM32G0B1xx
  1232. // For ARM Cortex M3/M4 CPUs, we have the optimized assembler version, that takes 43 cycles to execute
  1233. uint32_t flo = 0;
  1234. uint32_t fhi = bezier_AV * curr_step;
  1235. uint32_t t = fhi;
  1236. int32_t alo = bezier_F;
  1237. int32_t ahi = 0;
  1238. int32_t A = bezier_A;
  1239. int32_t B = bezier_B;
  1240. int32_t C = bezier_C;
  1241. __asm__ __volatile__(
  1242. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  1243. A("lsrs %[ahi],%[alo],#1") // a = F << 31 1 cycles
  1244. A("lsls %[alo],%[alo],#31") // 1 cycles
  1245. A("umull %[flo],%[fhi],%[fhi],%[t]") // f *= t 5 cycles [fhi:flo=64bits]
  1246. A("umull %[flo],%[fhi],%[fhi],%[t]") // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  1247. A("lsrs %[flo],%[fhi],#1") // 1 cycles [31bits]
  1248. A("smlal %[alo],%[ahi],%[flo],%[C]") // a+=(f>>33)*C; 5 cycles
  1249. A("umull %[flo],%[fhi],%[fhi],%[t]") // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  1250. A("lsrs %[flo],%[fhi],#1") // 1 cycles [31bits]
  1251. A("smlal %[alo],%[ahi],%[flo],%[B]") // a+=(f>>33)*B; 5 cycles
  1252. A("umull %[flo],%[fhi],%[fhi],%[t]") // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  1253. A("lsrs %[flo],%[fhi],#1") // f>>=33; 1 cycles [31bits]
  1254. A("smlal %[alo],%[ahi],%[flo],%[A]") // a+=(f>>33)*A; 5 cycles
  1255. A("lsrs %[alo],%[ahi],#6") // a>>=38 1 cycles
  1256. : [alo]"+r"( alo ) ,
  1257. [flo]"+r"( flo ) ,
  1258. [fhi]"+r"( fhi ) ,
  1259. [ahi]"+r"( ahi ) ,
  1260. [A]"+r"( A ) , // <== Note: Even if A, B, C, and t registers are INPUT ONLY
  1261. [B]"+r"( B ) , // GCC does bad optimizations on the code if we list them as
  1262. [C]"+r"( C ) , // such, breaking this function. So, to avoid that problem,
  1263. [t]"+r"( t ) // we list all registers as input-outputs.
  1264. :
  1265. : "cc"
  1266. );
  1267. return alo;
  1268. #else
  1269. // For non ARM targets, we provide a fallback implementation. Really doubt it
  1270. // will be useful, unless the processor is fast and 32bit
  1271. uint32_t t = bezier_AV * curr_step; // t: Range 0 - 1^32 = 32 bits
  1272. uint64_t f = t;
  1273. f *= t; // Range 32*2 = 64 bits (unsigned)
  1274. f >>= 32; // Range 32 bits (unsigned)
  1275. f *= t; // Range 32*2 = 64 bits (unsigned)
  1276. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  1277. int64_t acc = (int64_t) bezier_F << 31; // Range 63 bits (signed)
  1278. acc += ((uint32_t) f >> 1) * (int64_t) bezier_C; // Range 29bits + 31 = 60bits (plus sign)
  1279. f *= t; // Range 32*2 = 64 bits
  1280. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  1281. acc += ((uint32_t) f >> 1) * (int64_t) bezier_B; // Range 29bits + 31 = 60bits (plus sign)
  1282. f *= t; // Range 32*2 = 64 bits
  1283. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  1284. acc += ((uint32_t) f >> 1) * (int64_t) bezier_A; // Range 28bits + 31 = 59bits (plus sign)
  1285. acc >>= (31 + 7); // Range 24bits (plus sign)
  1286. return (int32_t) acc;
  1287. #endif
  1288. }
  1289. #endif
  1290. #endif // S_CURVE_ACCELERATION
  1291. /**
  1292. * Stepper Driver Interrupt
  1293. *
  1294. * Directly pulses the stepper motors at high frequency.
  1295. */
  1296. HAL_STEP_TIMER_ISR() {
  1297. HAL_timer_isr_prologue(MF_TIMER_STEP);
  1298. Stepper::isr();
  1299. HAL_timer_isr_epilogue(MF_TIMER_STEP);
  1300. }
  1301. #ifdef CPU_32_BIT
  1302. #define STEP_MULTIPLY(A,B) MultiU32X24toH32(A, B)
  1303. #else
  1304. #define STEP_MULTIPLY(A,B) MultiU24X32toH16(A, B)
  1305. #endif
  1306. void Stepper::isr() {
  1307. static uint32_t nextMainISR = 0; // Interval until the next main Stepper Pulse phase (0 = Now)
  1308. #ifndef __AVR__
  1309. // Disable interrupts, to avoid ISR preemption while we reprogram the period
  1310. // (AVR enters the ISR with global interrupts disabled, so no need to do it here)
  1311. hal.isr_off();
  1312. #endif
  1313. // Program timer compare for the maximum period, so it does NOT
  1314. // flag an interrupt while this ISR is running - So changes from small
  1315. // periods to big periods are respected and the timer does not reset to 0
  1316. HAL_timer_set_compare(MF_TIMER_STEP, hal_timer_t(HAL_TIMER_TYPE_MAX));
  1317. // Count of ticks for the next ISR
  1318. hal_timer_t next_isr_ticks = 0;
  1319. // Limit the amount of iterations
  1320. uint8_t max_loops = 10;
  1321. // We need this variable here to be able to use it in the following loop
  1322. hal_timer_t min_ticks;
  1323. do {
  1324. // Enable ISRs to reduce USART processing latency
  1325. hal.isr_on();
  1326. if (!nextMainISR) pulse_phase_isr(); // 0 = Do coordinated axes Stepper pulses
  1327. #if ENABLED(LIN_ADVANCE)
  1328. if (!nextAdvanceISR) { // 0 = Do Linear Advance E Stepper pulses
  1329. advance_isr();
  1330. nextAdvanceISR = la_interval;
  1331. }
  1332. else if (nextAdvanceISR == LA_ADV_NEVER) // Start LA steps if necessary
  1333. nextAdvanceISR = la_interval;
  1334. #endif
  1335. #if ENABLED(INTEGRATED_BABYSTEPPING)
  1336. const bool is_babystep = (nextBabystepISR == 0); // 0 = Do Babystepping (XY)Z pulses
  1337. if (is_babystep) nextBabystepISR = babystepping_isr();
  1338. #endif
  1339. // ^== Time critical. NOTHING besides pulse generation should be above here!!!
  1340. if (!nextMainISR) nextMainISR = block_phase_isr(); // Manage acc/deceleration, get next block
  1341. #if ENABLED(INTEGRATED_BABYSTEPPING)
  1342. if (is_babystep) // Avoid ANY stepping too soon after baby-stepping
  1343. NOLESS(nextMainISR, (BABYSTEP_TICKS) / 8); // FULL STOP for 125µs after a baby-step
  1344. if (nextBabystepISR != BABYSTEP_NEVER) // Avoid baby-stepping too close to axis Stepping
  1345. NOLESS(nextBabystepISR, nextMainISR / 2); // TODO: Only look at axes enabled for baby-stepping
  1346. #endif
  1347. // Get the interval to the next ISR call
  1348. const uint32_t interval = _MIN(
  1349. uint32_t(HAL_TIMER_TYPE_MAX), // Come back in a very long time
  1350. nextMainISR // Time until the next Pulse / Block phase
  1351. OPTARG(LIN_ADVANCE, nextAdvanceISR) // Come back early for Linear Advance?
  1352. OPTARG(INTEGRATED_BABYSTEPPING, nextBabystepISR) // Come back early for Babystepping?
  1353. );
  1354. //
  1355. // Compute remaining time for each ISR phase
  1356. // NEVER : The phase is idle
  1357. // Zero : The phase will occur on the next ISR call
  1358. // Non-zero : The phase will occur on a future ISR call
  1359. //
  1360. nextMainISR -= interval;
  1361. #if ENABLED(LIN_ADVANCE)
  1362. if (nextAdvanceISR != LA_ADV_NEVER) nextAdvanceISR -= interval;
  1363. #endif
  1364. #if ENABLED(INTEGRATED_BABYSTEPPING)
  1365. if (nextBabystepISR != BABYSTEP_NEVER) nextBabystepISR -= interval;
  1366. #endif
  1367. /**
  1368. * This needs to avoid a race-condition caused by interleaving
  1369. * of interrupts required by both the LA and Stepper algorithms.
  1370. *
  1371. * Assume the following tick times for stepper pulses:
  1372. * Stepper ISR (S): 1 1000 2000 3000 4000
  1373. * Linear Adv. (E): 10 1010 2010 3010 4010
  1374. *
  1375. * The current algorithm tries to interleave them, giving:
  1376. * 1:S 10:E 1000:S 1010:E 2000:S 2010:E 3000:S 3010:E 4000:S 4010:E
  1377. *
  1378. * Ideal timing would yield these delta periods:
  1379. * 1:S 9:E 990:S 10:E 990:S 10:E 990:S 10:E 990:S 10:E
  1380. *
  1381. * But, since each event must fire an ISR with a minimum duration, the
  1382. * minimum delta might be 900, so deltas under 900 get rounded up:
  1383. * 900:S d900:E d990:S d900:E d990:S d900:E d990:S d900:E d990:S d900:E
  1384. *
  1385. * It works, but divides the speed of all motors by half, leading to a sudden
  1386. * reduction to 1/2 speed! Such jumps in speed lead to lost steps (not even
  1387. * accounting for double/quad stepping, which makes it even worse).
  1388. */
  1389. // Compute the tick count for the next ISR
  1390. next_isr_ticks += interval;
  1391. /**
  1392. * The following section must be done with global interrupts disabled.
  1393. * We want nothing to interrupt it, as that could mess the calculations
  1394. * we do for the next value to program in the period register of the
  1395. * stepper timer and lead to skipped ISRs (if the value we happen to program
  1396. * is less than the current count due to something preempting between the
  1397. * read and the write of the new period value).
  1398. */
  1399. hal.isr_off();
  1400. /**
  1401. * Get the current tick value + margin
  1402. * Assuming at least 6µs between calls to this ISR...
  1403. * On AVR the ISR epilogue+prologue is estimated at 100 instructions - Give 8µs as margin
  1404. * On ARM the ISR epilogue+prologue is estimated at 20 instructions - Give 1µs as margin
  1405. */
  1406. min_ticks = HAL_timer_get_count(MF_TIMER_STEP) + hal_timer_t(
  1407. #ifdef __AVR__
  1408. 8
  1409. #else
  1410. 1
  1411. #endif
  1412. * (STEPPER_TIMER_TICKS_PER_US)
  1413. );
  1414. /**
  1415. * NB: If for some reason the stepper monopolizes the MPU, eventually the
  1416. * timer will wrap around (and so will 'next_isr_ticks'). So, limit the
  1417. * loop to 10 iterations. Beyond that, there's no way to ensure correct pulse
  1418. * timing, since the MCU isn't fast enough.
  1419. */
  1420. if (!--max_loops) next_isr_ticks = min_ticks;
  1421. // Advance pulses if not enough time to wait for the next ISR
  1422. } while (next_isr_ticks < min_ticks);
  1423. // Now 'next_isr_ticks' contains the period to the next Stepper ISR - And we are
  1424. // sure that the time has not arrived yet - Warrantied by the scheduler
  1425. // Set the next ISR to fire at the proper time
  1426. HAL_timer_set_compare(MF_TIMER_STEP, hal_timer_t(next_isr_ticks));
  1427. // Don't forget to finally reenable interrupts
  1428. hal.isr_on();
  1429. }
  1430. #if MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE
  1431. #define ISR_PULSE_CONTROL 1
  1432. #endif
  1433. #if ISR_PULSE_CONTROL && DISABLED(I2S_STEPPER_STREAM)
  1434. #define ISR_MULTI_STEPS 1
  1435. #endif
  1436. /**
  1437. * This phase of the ISR should ONLY create the pulses for the steppers.
  1438. * This prevents jitter caused by the interval between the start of the
  1439. * interrupt and the start of the pulses. DON'T add any logic ahead of the
  1440. * call to this method that might cause variation in the timing. The aim
  1441. * is to keep pulse timing as regular as possible.
  1442. */
  1443. void Stepper::pulse_phase_isr() {
  1444. // If we must abort the current block, do so!
  1445. if (abort_current_block) {
  1446. abort_current_block = false;
  1447. if (current_block) discard_current_block();
  1448. }
  1449. // If there is no current block, do nothing
  1450. if (!current_block) return;
  1451. // Skipping step processing causes motion to freeze
  1452. if (TERN0(FREEZE_FEATURE, frozen)) return;
  1453. // Count of pending loops and events for this iteration
  1454. const uint32_t pending_events = step_event_count - step_events_completed;
  1455. uint8_t events_to_do = _MIN(pending_events, steps_per_isr);
  1456. // Just update the value we will get at the end of the loop
  1457. step_events_completed += events_to_do;
  1458. // Take multiple steps per interrupt (For high speed moves)
  1459. #if ISR_MULTI_STEPS
  1460. bool firstStep = true;
  1461. USING_TIMED_PULSE();
  1462. #endif
  1463. xyze_bool_t step_needed{0};
  1464. do {
  1465. #define _APPLY_STEP(AXIS, INV, ALWAYS) AXIS ##_APPLY_STEP(INV, ALWAYS)
  1466. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  1467. // Determine if a pulse is needed using Bresenham
  1468. #define PULSE_PREP(AXIS) do{ \
  1469. delta_error[_AXIS(AXIS)] += advance_dividend[_AXIS(AXIS)]; \
  1470. step_needed[_AXIS(AXIS)] = (delta_error[_AXIS(AXIS)] >= 0); \
  1471. if (step_needed[_AXIS(AXIS)]) { \
  1472. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  1473. delta_error[_AXIS(AXIS)] -= advance_divisor; \
  1474. } \
  1475. }while(0)
  1476. // Start an active pulse if needed
  1477. #define PULSE_START(AXIS) do{ \
  1478. if (step_needed[_AXIS(AXIS)]) { \
  1479. _APPLY_STEP(AXIS, !_INVERT_STEP_PIN(AXIS), 0); \
  1480. } \
  1481. }while(0)
  1482. // Stop an active pulse if needed
  1483. #define PULSE_STOP(AXIS) do { \
  1484. if (step_needed[_AXIS(AXIS)]) { \
  1485. _APPLY_STEP(AXIS, _INVERT_STEP_PIN(AXIS), 0); \
  1486. } \
  1487. }while(0)
  1488. // Direct Stepping page?
  1489. const bool is_page = current_block->is_page();
  1490. #if ENABLED(DIRECT_STEPPING)
  1491. // Direct stepping is currently not ready for HAS_I_AXIS
  1492. if (is_page) {
  1493. #if STEPPER_PAGE_FORMAT == SP_4x4D_128
  1494. #define PAGE_SEGMENT_UPDATE(AXIS, VALUE) do{ \
  1495. if ((VALUE) < 7) SBI(dm, _AXIS(AXIS)); \
  1496. else if ((VALUE) > 7) CBI(dm, _AXIS(AXIS)); \
  1497. page_step_state.sd[_AXIS(AXIS)] = VALUE; \
  1498. page_step_state.bd[_AXIS(AXIS)] += VALUE; \
  1499. }while(0)
  1500. #define PAGE_PULSE_PREP(AXIS) do{ \
  1501. step_needed[_AXIS(AXIS)] = \
  1502. pgm_read_byte(&segment_table[page_step_state.sd[_AXIS(AXIS)]][page_step_state.segment_steps & 0x7]); \
  1503. }while(0)
  1504. switch (page_step_state.segment_steps) {
  1505. case DirectStepping::Config::SEGMENT_STEPS:
  1506. page_step_state.segment_idx += 2;
  1507. page_step_state.segment_steps = 0;
  1508. // fallthru
  1509. case 0: {
  1510. const uint8_t low = page_step_state.page[page_step_state.segment_idx],
  1511. high = page_step_state.page[page_step_state.segment_idx + 1];
  1512. axis_bits_t dm = last_direction_bits;
  1513. PAGE_SEGMENT_UPDATE(X, low >> 4);
  1514. PAGE_SEGMENT_UPDATE(Y, low & 0xF);
  1515. PAGE_SEGMENT_UPDATE(Z, high >> 4);
  1516. PAGE_SEGMENT_UPDATE(E, high & 0xF);
  1517. if (dm != last_direction_bits)
  1518. set_directions(dm);
  1519. } break;
  1520. default: break;
  1521. }
  1522. PAGE_PULSE_PREP(X);
  1523. PAGE_PULSE_PREP(Y);
  1524. PAGE_PULSE_PREP(Z);
  1525. TERN_(HAS_EXTRUDERS, PAGE_PULSE_PREP(E));
  1526. page_step_state.segment_steps++;
  1527. #elif STEPPER_PAGE_FORMAT == SP_4x2_256
  1528. #define PAGE_SEGMENT_UPDATE(AXIS, VALUE) \
  1529. page_step_state.sd[_AXIS(AXIS)] = VALUE; \
  1530. page_step_state.bd[_AXIS(AXIS)] += VALUE;
  1531. #define PAGE_PULSE_PREP(AXIS) do{ \
  1532. step_needed[_AXIS(AXIS)] = \
  1533. pgm_read_byte(&segment_table[page_step_state.sd[_AXIS(AXIS)]][page_step_state.segment_steps & 0x3]); \
  1534. }while(0)
  1535. switch (page_step_state.segment_steps) {
  1536. case DirectStepping::Config::SEGMENT_STEPS:
  1537. page_step_state.segment_idx++;
  1538. page_step_state.segment_steps = 0;
  1539. // fallthru
  1540. case 0: {
  1541. const uint8_t b = page_step_state.page[page_step_state.segment_idx];
  1542. PAGE_SEGMENT_UPDATE(X, (b >> 6) & 0x3);
  1543. PAGE_SEGMENT_UPDATE(Y, (b >> 4) & 0x3);
  1544. PAGE_SEGMENT_UPDATE(Z, (b >> 2) & 0x3);
  1545. PAGE_SEGMENT_UPDATE(E, (b >> 0) & 0x3);
  1546. } break;
  1547. default: break;
  1548. }
  1549. PAGE_PULSE_PREP(X);
  1550. PAGE_PULSE_PREP(Y);
  1551. PAGE_PULSE_PREP(Z);
  1552. TERN_(HAS_EXTRUDERS, PAGE_PULSE_PREP(E));
  1553. page_step_state.segment_steps++;
  1554. #elif STEPPER_PAGE_FORMAT == SP_4x1_512
  1555. #define PAGE_PULSE_PREP(AXIS, BITS) do{ \
  1556. step_needed[_AXIS(AXIS)] = (steps >> BITS) & 0x1; \
  1557. if (step_needed[_AXIS(AXIS)]) \
  1558. page_step_state.bd[_AXIS(AXIS)]++; \
  1559. }while(0)
  1560. uint8_t steps = page_step_state.page[page_step_state.segment_idx >> 1];
  1561. if (page_step_state.segment_idx & 0x1) steps >>= 4;
  1562. PAGE_PULSE_PREP(X, 3);
  1563. PAGE_PULSE_PREP(Y, 2);
  1564. PAGE_PULSE_PREP(Z, 1);
  1565. PAGE_PULSE_PREP(E, 0);
  1566. page_step_state.segment_idx++;
  1567. #else
  1568. #error "Unknown direct stepping page format!"
  1569. #endif
  1570. }
  1571. #endif // DIRECT_STEPPING
  1572. if (!is_page) {
  1573. // Determine if pulses are needed
  1574. #if HAS_X_STEP
  1575. PULSE_PREP(X);
  1576. #endif
  1577. #if HAS_Y_STEP
  1578. PULSE_PREP(Y);
  1579. #endif
  1580. #if HAS_Z_STEP
  1581. PULSE_PREP(Z);
  1582. #endif
  1583. #if HAS_I_STEP
  1584. PULSE_PREP(I);
  1585. #endif
  1586. #if HAS_J_STEP
  1587. PULSE_PREP(J);
  1588. #endif
  1589. #if HAS_K_STEP
  1590. PULSE_PREP(K);
  1591. #endif
  1592. #if HAS_U_STEP
  1593. PULSE_PREP(U);
  1594. #endif
  1595. #if HAS_V_STEP
  1596. PULSE_PREP(V);
  1597. #endif
  1598. #if HAS_W_STEP
  1599. PULSE_PREP(W);
  1600. #endif
  1601. #if EITHER(HAS_E0_STEP, MIXING_EXTRUDER)
  1602. PULSE_PREP(E);
  1603. #if ENABLED(LIN_ADVANCE)
  1604. if (step_needed.e && current_block->la_advance_rate) {
  1605. // don't actually step here, but do subtract movements steps
  1606. // from the linear advance step count
  1607. step_needed.e = false;
  1608. count_position.e -= count_direction.e;
  1609. la_advance_steps--;
  1610. }
  1611. #endif
  1612. #endif
  1613. }
  1614. #if ISR_MULTI_STEPS
  1615. if (firstStep)
  1616. firstStep = false;
  1617. else
  1618. AWAIT_LOW_PULSE();
  1619. #endif
  1620. // Pulse start
  1621. #if HAS_X_STEP
  1622. PULSE_START(X);
  1623. #endif
  1624. #if HAS_Y_STEP
  1625. PULSE_START(Y);
  1626. #endif
  1627. #if HAS_Z_STEP
  1628. PULSE_START(Z);
  1629. #endif
  1630. #if HAS_I_STEP
  1631. PULSE_START(I);
  1632. #endif
  1633. #if HAS_J_STEP
  1634. PULSE_START(J);
  1635. #endif
  1636. #if HAS_K_STEP
  1637. PULSE_START(K);
  1638. #endif
  1639. #if HAS_U_STEP
  1640. PULSE_START(U);
  1641. #endif
  1642. #if HAS_V_STEP
  1643. PULSE_START(V);
  1644. #endif
  1645. #if HAS_W_STEP
  1646. PULSE_START(W);
  1647. #endif
  1648. #if ENABLED(MIXING_EXTRUDER)
  1649. if (step_needed.e) E_STEP_WRITE(mixer.get_next_stepper(), !INVERT_E_STEP_PIN);
  1650. #elif HAS_E0_STEP
  1651. PULSE_START(E);
  1652. #endif
  1653. TERN_(I2S_STEPPER_STREAM, i2s_push_sample());
  1654. // TODO: need to deal with MINIMUM_STEPPER_PULSE over i2s
  1655. #if ISR_MULTI_STEPS
  1656. START_HIGH_PULSE();
  1657. AWAIT_HIGH_PULSE();
  1658. #endif
  1659. // Pulse stop
  1660. #if HAS_X_STEP
  1661. PULSE_STOP(X);
  1662. #endif
  1663. #if HAS_Y_STEP
  1664. PULSE_STOP(Y);
  1665. #endif
  1666. #if HAS_Z_STEP
  1667. PULSE_STOP(Z);
  1668. #endif
  1669. #if HAS_I_STEP
  1670. PULSE_STOP(I);
  1671. #endif
  1672. #if HAS_J_STEP
  1673. PULSE_STOP(J);
  1674. #endif
  1675. #if HAS_K_STEP
  1676. PULSE_STOP(K);
  1677. #endif
  1678. #if HAS_U_STEP
  1679. PULSE_STOP(U);
  1680. #endif
  1681. #if HAS_V_STEP
  1682. PULSE_STOP(V);
  1683. #endif
  1684. #if HAS_W_STEP
  1685. PULSE_STOP(W);
  1686. #endif
  1687. #if ENABLED(MIXING_EXTRUDER)
  1688. if (step_needed.e) E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
  1689. #elif HAS_E0_STEP
  1690. PULSE_STOP(E);
  1691. #endif
  1692. #if ISR_MULTI_STEPS
  1693. if (events_to_do) START_LOW_PULSE();
  1694. #endif
  1695. } while (--events_to_do);
  1696. }
  1697. // Calculate timer interval, with all limits applied.
  1698. uint32_t Stepper::calc_timer_interval(uint32_t step_rate) {
  1699. #ifdef CPU_32_BIT
  1700. // In case of high-performance processor, it is able to calculate in real-time
  1701. return uint32_t(STEPPER_TIMER_RATE) / step_rate;
  1702. #else
  1703. // AVR is able to keep up at 30khz Stepping ISR rate.
  1704. constexpr uint32_t min_step_rate = (F_CPU) / 500000U;
  1705. if (step_rate <= min_step_rate) {
  1706. step_rate = 0;
  1707. uintptr_t table_address = (uintptr_t)&speed_lookuptable_slow[0][0];
  1708. return uint16_t(pgm_read_word(table_address));
  1709. }
  1710. else {
  1711. step_rate -= min_step_rate; // Correct for minimal speed
  1712. if (step_rate >= 0x0800) { // higher step rate
  1713. const uint8_t rate_mod_256 = (step_rate & 0x00FF);
  1714. const uintptr_t table_address = uintptr_t(&speed_lookuptable_fast[uint8_t(step_rate >> 8)][0]),
  1715. gain = uint16_t(pgm_read_word(table_address + 2));
  1716. return uint16_t(pgm_read_word(table_address)) - MultiU16X8toH16(rate_mod_256, gain);
  1717. }
  1718. else { // lower step rates
  1719. uintptr_t table_address = uintptr_t(&speed_lookuptable_slow[0][0]);
  1720. table_address += (step_rate >> 1) & 0xFFFC;
  1721. return uint16_t(pgm_read_word(table_address))
  1722. - ((uint16_t(pgm_read_word(table_address + 2)) * uint8_t(step_rate & 0x0007)) >> 3);
  1723. }
  1724. }
  1725. #endif
  1726. }
  1727. // Get the timer interval and the number of loops to perform per tick
  1728. uint32_t Stepper::calc_timer_interval(uint32_t step_rate, uint8_t &loops) {
  1729. uint8_t multistep = 1;
  1730. #if DISABLED(DISABLE_MULTI_STEPPING)
  1731. // The stepping frequency limits for each multistepping rate
  1732. static const uint32_t limit[] PROGMEM = {
  1733. ( MAX_STEP_ISR_FREQUENCY_1X ),
  1734. ( MAX_STEP_ISR_FREQUENCY_2X >> 1),
  1735. ( MAX_STEP_ISR_FREQUENCY_4X >> 2),
  1736. ( MAX_STEP_ISR_FREQUENCY_8X >> 3),
  1737. ( MAX_STEP_ISR_FREQUENCY_16X >> 4),
  1738. ( MAX_STEP_ISR_FREQUENCY_32X >> 5),
  1739. ( MAX_STEP_ISR_FREQUENCY_64X >> 6),
  1740. (MAX_STEP_ISR_FREQUENCY_128X >> 7)
  1741. };
  1742. // Select the proper multistepping
  1743. uint8_t idx = 0;
  1744. while (idx < 7 && step_rate > (uint32_t)pgm_read_dword(&limit[idx])) {
  1745. step_rate >>= 1;
  1746. multistep <<= 1;
  1747. ++idx;
  1748. };
  1749. #else
  1750. NOMORE(step_rate, uint32_t(MAX_STEP_ISR_FREQUENCY_1X));
  1751. #endif
  1752. loops = multistep;
  1753. return calc_timer_interval(step_rate);
  1754. }
  1755. // This is the last half of the stepper interrupt: This one processes and
  1756. // properly schedules blocks from the planner. This is executed after creating
  1757. // the step pulses, so it is not time critical, as pulses are already done.
  1758. uint32_t Stepper::block_phase_isr() {
  1759. // If no queued movements, just wait 1ms for the next block
  1760. uint32_t interval = (STEPPER_TIMER_RATE) / 1000UL;
  1761. // If there is a current block
  1762. if (current_block) {
  1763. // If current block is finished, reset pointer and finalize state
  1764. if (step_events_completed >= step_event_count) {
  1765. #if ENABLED(DIRECT_STEPPING)
  1766. // Direct stepping is currently not ready for HAS_I_AXIS
  1767. #if STEPPER_PAGE_FORMAT == SP_4x4D_128
  1768. #define PAGE_SEGMENT_UPDATE_POS(AXIS) \
  1769. count_position[_AXIS(AXIS)] += page_step_state.bd[_AXIS(AXIS)] - 128 * 7;
  1770. #elif STEPPER_PAGE_FORMAT == SP_4x1_512 || STEPPER_PAGE_FORMAT == SP_4x2_256
  1771. #define PAGE_SEGMENT_UPDATE_POS(AXIS) \
  1772. count_position[_AXIS(AXIS)] += page_step_state.bd[_AXIS(AXIS)] * count_direction[_AXIS(AXIS)];
  1773. #endif
  1774. if (current_block->is_page()) {
  1775. PAGE_SEGMENT_UPDATE_POS(X);
  1776. PAGE_SEGMENT_UPDATE_POS(Y);
  1777. PAGE_SEGMENT_UPDATE_POS(Z);
  1778. PAGE_SEGMENT_UPDATE_POS(E);
  1779. }
  1780. #endif
  1781. TERN_(HAS_FILAMENT_RUNOUT_DISTANCE, runout.block_completed(current_block));
  1782. discard_current_block();
  1783. }
  1784. else {
  1785. // Step events not completed yet...
  1786. // Are we in acceleration phase ?
  1787. if (step_events_completed <= accelerate_until) { // Calculate new timer value
  1788. #if ENABLED(S_CURVE_ACCELERATION)
  1789. // Get the next speed to use (Jerk limited!)
  1790. uint32_t acc_step_rate = acceleration_time < current_block->acceleration_time
  1791. ? _eval_bezier_curve(acceleration_time)
  1792. : current_block->cruise_rate;
  1793. #else
  1794. acc_step_rate = STEP_MULTIPLY(acceleration_time, current_block->acceleration_rate) + current_block->initial_rate;
  1795. NOMORE(acc_step_rate, current_block->nominal_rate);
  1796. #endif
  1797. // acc_step_rate is in steps/second
  1798. // step_rate to timer interval and steps per stepper isr
  1799. interval = calc_timer_interval(acc_step_rate << oversampling_factor, steps_per_isr);
  1800. acceleration_time += interval;
  1801. #if ENABLED(LIN_ADVANCE)
  1802. if (current_block->la_advance_rate) {
  1803. const uint32_t la_step_rate = la_advance_steps < current_block->max_adv_steps ? current_block->la_advance_rate : 0;
  1804. la_interval = calc_timer_interval(acc_step_rate + la_step_rate) << current_block->la_scaling;
  1805. }
  1806. #endif
  1807. /**
  1808. * Adjust Laser Power - Accelerating
  1809. *
  1810. * isPowered - True when a move is powered.
  1811. * isEnabled - laser power is active.
  1812. *
  1813. * Laser power variables are calulated and stored in this block by the planner code.
  1814. * trap_ramp_active_pwr - the active power in this block across accel or decel trap steps.
  1815. * trap_ramp_entry_incr - holds the precalculated value to increase the current power per accel step.
  1816. *
  1817. * Apply the starting active power and then increase power per step by the trap_ramp_entry_incr value if positive.
  1818. */
  1819. #if ENABLED(LASER_POWER_TRAP)
  1820. if (cutter.cutter_mode == CUTTER_MODE_CONTINUOUS) {
  1821. if (planner.laser_inline.status.isPowered && planner.laser_inline.status.isEnabled) {
  1822. if (current_block->laser.trap_ramp_entry_incr > 0) {
  1823. cutter.apply_power(current_block->laser.trap_ramp_active_pwr);
  1824. current_block->laser.trap_ramp_active_pwr += current_block->laser.trap_ramp_entry_incr;
  1825. }
  1826. }
  1827. // Not a powered move.
  1828. else cutter.apply_power(0);
  1829. }
  1830. #endif
  1831. }
  1832. // Are we in Deceleration phase ?
  1833. else if (step_events_completed > decelerate_after) {
  1834. uint32_t step_rate;
  1835. #if ENABLED(S_CURVE_ACCELERATION)
  1836. // If this is the 1st time we process the 2nd half of the trapezoid...
  1837. if (!bezier_2nd_half) {
  1838. // Initialize the Bézier speed curve
  1839. _calc_bezier_curve_coeffs(current_block->cruise_rate, current_block->final_rate, current_block->deceleration_time_inverse);
  1840. bezier_2nd_half = true;
  1841. // The first point starts at cruise rate. Just save evaluation of the Bézier curve
  1842. step_rate = current_block->cruise_rate;
  1843. }
  1844. else {
  1845. // Calculate the next speed to use
  1846. step_rate = deceleration_time < current_block->deceleration_time
  1847. ? _eval_bezier_curve(deceleration_time)
  1848. : current_block->final_rate;
  1849. }
  1850. #else
  1851. // Using the old trapezoidal control
  1852. step_rate = STEP_MULTIPLY(deceleration_time, current_block->acceleration_rate);
  1853. if (step_rate < acc_step_rate) { // Still decelerating?
  1854. step_rate = acc_step_rate - step_rate;
  1855. NOLESS(step_rate, current_block->final_rate);
  1856. }
  1857. else
  1858. step_rate = current_block->final_rate;
  1859. #endif
  1860. // step_rate to timer interval and steps per stepper isr
  1861. interval = calc_timer_interval(step_rate << oversampling_factor, steps_per_isr);
  1862. deceleration_time += interval;
  1863. #if ENABLED(LIN_ADVANCE)
  1864. if (current_block->la_advance_rate) {
  1865. const uint32_t la_step_rate = la_advance_steps > current_block->final_adv_steps ? current_block->la_advance_rate : 0;
  1866. if (la_step_rate != step_rate) {
  1867. bool reverse_e = la_step_rate > step_rate;
  1868. la_interval = calc_timer_interval(reverse_e ? la_step_rate - step_rate : step_rate - la_step_rate) << current_block->la_scaling;
  1869. if (reverse_e != motor_direction(E_AXIS)) {
  1870. TBI(last_direction_bits, E_AXIS);
  1871. count_direction.e = -count_direction.e;
  1872. DIR_WAIT_BEFORE();
  1873. if (reverse_e) {
  1874. #if ENABLED(MIXING_EXTRUDER)
  1875. MIXER_STEPPER_LOOP(j) REV_E_DIR(j);
  1876. #else
  1877. REV_E_DIR(stepper_extruder);
  1878. #endif
  1879. }
  1880. else {
  1881. #if ENABLED(MIXING_EXTRUDER)
  1882. MIXER_STEPPER_LOOP(j) NORM_E_DIR(j);
  1883. #else
  1884. NORM_E_DIR(stepper_extruder);
  1885. #endif
  1886. }
  1887. DIR_WAIT_AFTER();
  1888. }
  1889. }
  1890. }
  1891. #endif // LIN_ADVANCE
  1892. /*
  1893. * Adjust Laser Power - Decelerating
  1894. * trap_ramp_entry_decr - holds the precalculated value to decrease the current power per decel step.
  1895. */
  1896. #if ENABLED(LASER_POWER_TRAP)
  1897. if (cutter.cutter_mode == CUTTER_MODE_CONTINUOUS) {
  1898. if (planner.laser_inline.status.isPowered && planner.laser_inline.status.isEnabled) {
  1899. if (current_block->laser.trap_ramp_exit_decr > 0) {
  1900. current_block->laser.trap_ramp_active_pwr -= current_block->laser.trap_ramp_exit_decr;
  1901. cutter.apply_power(current_block->laser.trap_ramp_active_pwr);
  1902. }
  1903. // Not a powered move.
  1904. else cutter.apply_power(0);
  1905. }
  1906. }
  1907. #endif
  1908. }
  1909. else { // Must be in cruise phase otherwise
  1910. // Calculate the ticks_nominal for this nominal speed, if not done yet
  1911. if (ticks_nominal < 0) {
  1912. // step_rate to timer interval and loops for the nominal speed
  1913. ticks_nominal = calc_timer_interval(current_block->nominal_rate << oversampling_factor, steps_per_isr);
  1914. #if ENABLED(LIN_ADVANCE)
  1915. if (current_block->la_advance_rate)
  1916. la_interval = calc_timer_interval(current_block->nominal_rate) << current_block->la_scaling;
  1917. #endif
  1918. }
  1919. // The timer interval is just the nominal value for the nominal speed
  1920. interval = ticks_nominal;
  1921. }
  1922. /**
  1923. * Adjust Laser Power - Cruise
  1924. * power - direct or floor adjusted active laser power.
  1925. */
  1926. #if ENABLED(LASER_POWER_TRAP)
  1927. if (cutter.cutter_mode == CUTTER_MODE_CONTINUOUS) {
  1928. if (step_events_completed + 1 == accelerate_until) {
  1929. if (planner.laser_inline.status.isPowered && planner.laser_inline.status.isEnabled) {
  1930. if (current_block->laser.trap_ramp_entry_incr > 0) {
  1931. current_block->laser.trap_ramp_active_pwr = current_block->laser.power;
  1932. cutter.apply_power(current_block->laser.power);
  1933. }
  1934. }
  1935. // Not a powered move.
  1936. else cutter.apply_power(0);
  1937. }
  1938. }
  1939. #endif
  1940. }
  1941. #if ENABLED(LASER_FEATURE)
  1942. /**
  1943. * CUTTER_MODE_DYNAMIC is experimental and developing.
  1944. * Super-fast method to dynamically adjust the laser power OCR value based on the input feedrate in mm-per-minute.
  1945. * TODO: Set up Min/Max OCR offsets to allow tuning and scaling of various lasers.
  1946. * TODO: Integrate accel/decel +-rate into the dynamic laser power calc.
  1947. */
  1948. if (cutter.cutter_mode == CUTTER_MODE_DYNAMIC
  1949. && planner.laser_inline.status.isPowered // isPowered flag set on any parsed G1, G2, G3, or G5 move; cleared on any others.
  1950. && cutter.last_block_power != current_block->laser.power // Prevent constant update without change
  1951. ) {
  1952. cutter.apply_power(current_block->laser.power);
  1953. cutter.last_block_power = current_block->laser.power;
  1954. }
  1955. #endif
  1956. }
  1957. else { // !current_block
  1958. #if ENABLED(LASER_FEATURE)
  1959. if (cutter.cutter_mode == CUTTER_MODE_DYNAMIC)
  1960. cutter.apply_power(0); // No movement in dynamic mode so turn Laser off
  1961. #endif
  1962. }
  1963. // If there is no current block at this point, attempt to pop one from the buffer
  1964. // and prepare its movement
  1965. if (!current_block) {
  1966. // Anything in the buffer?
  1967. if ((current_block = planner.get_current_block())) {
  1968. // Sync block? Sync the stepper counts or fan speeds and return
  1969. while (current_block->is_sync()) {
  1970. #if ENABLED(LASER_POWER_SYNC)
  1971. if (cutter.cutter_mode == CUTTER_MODE_CONTINUOUS) {
  1972. if (current_block->is_pwr_sync()) {
  1973. planner.laser_inline.status.isSyncPower = true;
  1974. cutter.apply_power(current_block->laser.power);
  1975. }
  1976. }
  1977. #endif
  1978. TERN_(LASER_SYNCHRONOUS_M106_M107, if (current_block->is_fan_sync()) planner.sync_fan_speeds(current_block->fan_speed));
  1979. if (!(current_block->is_fan_sync() || current_block->is_pwr_sync())) _set_position(current_block->position);
  1980. discard_current_block();
  1981. // Try to get a new block
  1982. if (!(current_block = planner.get_current_block()))
  1983. return interval; // No more queued movements!
  1984. }
  1985. // For non-inline cutter, grossly apply power
  1986. #if HAS_CUTTER
  1987. if (cutter.cutter_mode == CUTTER_MODE_STANDARD) {
  1988. cutter.apply_power(current_block->cutter_power);
  1989. }
  1990. #endif
  1991. #if ENABLED(POWER_LOSS_RECOVERY)
  1992. recovery.info.sdpos = current_block->sdpos;
  1993. recovery.info.current_position = current_block->start_position;
  1994. #endif
  1995. #if ENABLED(DIRECT_STEPPING)
  1996. if (current_block->is_page()) {
  1997. page_step_state.segment_steps = 0;
  1998. page_step_state.segment_idx = 0;
  1999. page_step_state.page = page_manager.get_page(current_block->page_idx);
  2000. page_step_state.bd.reset();
  2001. if (DirectStepping::Config::DIRECTIONAL)
  2002. current_block->direction_bits = last_direction_bits;
  2003. if (!page_step_state.page) {
  2004. discard_current_block();
  2005. return interval;
  2006. }
  2007. }
  2008. #endif
  2009. // Flag all moving axes for proper endstop handling
  2010. #if IS_CORE
  2011. // Define conditions for checking endstops
  2012. #define S_(N) current_block->steps[CORE_AXIS_##N]
  2013. #define D_(N) TEST(current_block->direction_bits, CORE_AXIS_##N)
  2014. #endif
  2015. #if CORE_IS_XY || CORE_IS_XZ
  2016. /**
  2017. * Head direction in -X axis for CoreXY and CoreXZ bots.
  2018. *
  2019. * If steps differ, both axes are moving.
  2020. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z, handled below)
  2021. * If DeltaA == DeltaB, the movement is only in the 1st axis (X)
  2022. */
  2023. #if EITHER(COREXY, COREXZ)
  2024. #define X_CMP(A,B) ((A)==(B))
  2025. #else
  2026. #define X_CMP(A,B) ((A)!=(B))
  2027. #endif
  2028. #define X_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && X_CMP(D_(1),D_(2))) )
  2029. #elif ENABLED(MARKFORGED_XY)
  2030. #define X_MOVE_TEST (current_block->steps.a != current_block->steps.b)
  2031. #else
  2032. #define X_MOVE_TEST !!current_block->steps.a
  2033. #endif
  2034. #if CORE_IS_XY || CORE_IS_YZ
  2035. /**
  2036. * Head direction in -Y axis for CoreXY / CoreYZ bots.
  2037. *
  2038. * If steps differ, both axes are moving
  2039. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y)
  2040. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z)
  2041. */
  2042. #if EITHER(COREYX, COREYZ)
  2043. #define Y_CMP(A,B) ((A)==(B))
  2044. #else
  2045. #define Y_CMP(A,B) ((A)!=(B))
  2046. #endif
  2047. #define Y_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && Y_CMP(D_(1),D_(2))) )
  2048. #elif ENABLED(MARKFORGED_YX)
  2049. #define Y_MOVE_TEST (current_block->steps.a != current_block->steps.b)
  2050. #else
  2051. #define Y_MOVE_TEST !!current_block->steps.b
  2052. #endif
  2053. #if CORE_IS_XZ || CORE_IS_YZ
  2054. /**
  2055. * Head direction in -Z axis for CoreXZ or CoreYZ bots.
  2056. *
  2057. * If steps differ, both axes are moving
  2058. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y, already handled above)
  2059. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Z)
  2060. */
  2061. #if EITHER(COREZX, COREZY)
  2062. #define Z_CMP(A,B) ((A)==(B))
  2063. #else
  2064. #define Z_CMP(A,B) ((A)!=(B))
  2065. #endif
  2066. #define Z_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && Z_CMP(D_(1),D_(2))) )
  2067. #else
  2068. #define Z_MOVE_TEST !!current_block->steps.c
  2069. #endif
  2070. axis_bits_t axis_bits = 0;
  2071. NUM_AXIS_CODE(
  2072. if (X_MOVE_TEST) SBI(axis_bits, A_AXIS),
  2073. if (Y_MOVE_TEST) SBI(axis_bits, B_AXIS),
  2074. if (Z_MOVE_TEST) SBI(axis_bits, C_AXIS),
  2075. if (current_block->steps.i) SBI(axis_bits, I_AXIS),
  2076. if (current_block->steps.j) SBI(axis_bits, J_AXIS),
  2077. if (current_block->steps.k) SBI(axis_bits, K_AXIS),
  2078. if (current_block->steps.u) SBI(axis_bits, U_AXIS),
  2079. if (current_block->steps.v) SBI(axis_bits, V_AXIS),
  2080. if (current_block->steps.w) SBI(axis_bits, W_AXIS)
  2081. );
  2082. //if (current_block->steps.e) SBI(axis_bits, E_AXIS);
  2083. //if (current_block->steps.a) SBI(axis_bits, X_HEAD);
  2084. //if (current_block->steps.b) SBI(axis_bits, Y_HEAD);
  2085. //if (current_block->steps.c) SBI(axis_bits, Z_HEAD);
  2086. axis_did_move = axis_bits;
  2087. // No acceleration / deceleration time elapsed so far
  2088. acceleration_time = deceleration_time = 0;
  2089. #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
  2090. uint8_t oversampling = 0; // Assume no axis smoothing (via oversampling)
  2091. // Decide if axis smoothing is possible
  2092. uint32_t max_rate = current_block->nominal_rate; // Get the step event rate
  2093. while (max_rate < MIN_STEP_ISR_FREQUENCY) { // As long as more ISRs are possible...
  2094. max_rate <<= 1; // Try to double the rate
  2095. if (max_rate < MIN_STEP_ISR_FREQUENCY) // Don't exceed the estimated ISR limit
  2096. ++oversampling; // Increase the oversampling (used for left-shift)
  2097. }
  2098. oversampling_factor = oversampling; // For all timer interval calculations
  2099. #else
  2100. constexpr uint8_t oversampling = 0;
  2101. #endif
  2102. // Based on the oversampling factor, do the calculations
  2103. step_event_count = current_block->step_event_count << oversampling;
  2104. // Initialize Bresenham delta errors to 1/2
  2105. delta_error = TERN_(LIN_ADVANCE, la_delta_error =) -int32_t(step_event_count);
  2106. // Calculate Bresenham dividends and divisors
  2107. advance_dividend = current_block->steps << 1;
  2108. advance_divisor = step_event_count << 1;
  2109. // No step events completed so far
  2110. step_events_completed = 0;
  2111. // Compute the acceleration and deceleration points
  2112. accelerate_until = current_block->accelerate_until << oversampling;
  2113. decelerate_after = current_block->decelerate_after << oversampling;
  2114. TERN_(MIXING_EXTRUDER, mixer.stepper_setup(current_block->b_color));
  2115. E_TERN_(stepper_extruder = current_block->extruder);
  2116. // Initialize the trapezoid generator from the current block.
  2117. #if ENABLED(LIN_ADVANCE)
  2118. #if DISABLED(MIXING_EXTRUDER) && E_STEPPERS > 1
  2119. // If the now active extruder wasn't in use during the last move, its pressure is most likely gone.
  2120. if (stepper_extruder != last_moved_extruder) la_advance_steps = 0;
  2121. #endif
  2122. if (current_block->la_advance_rate) {
  2123. // apply LA scaling and discount the effect of frequency scaling
  2124. la_dividend = (advance_dividend.e << current_block->la_scaling) << oversampling;
  2125. }
  2126. #endif
  2127. if ( ENABLED(DUAL_X_CARRIAGE) // TODO: Find out why this fixes "jittery" small circles
  2128. || current_block->direction_bits != last_direction_bits
  2129. || TERN(MIXING_EXTRUDER, false, stepper_extruder != last_moved_extruder)
  2130. ) {
  2131. E_TERN_(last_moved_extruder = stepper_extruder);
  2132. set_directions(current_block->direction_bits);
  2133. }
  2134. #if ENABLED(LASER_FEATURE)
  2135. if (cutter.cutter_mode == CUTTER_MODE_CONTINUOUS) { // Planner controls the laser
  2136. if (planner.laser_inline.status.isSyncPower)
  2137. // If the previous block was a M3 sync power then skip the trap power init otherwise it will 0 the sync power.
  2138. planner.laser_inline.status.isSyncPower = false; // Clear the flag to process subsequent trap calc's.
  2139. else if (current_block->laser.status.isEnabled) {
  2140. #if ENABLED(LASER_POWER_TRAP)
  2141. TERN_(DEBUG_LASER_TRAP, SERIAL_ECHO_MSG("InitTrapPwr:",current_block->laser.trap_ramp_active_pwr));
  2142. cutter.apply_power(current_block->laser.status.isPowered ? current_block->laser.trap_ramp_active_pwr : 0);
  2143. #else
  2144. TERN_(DEBUG_CUTTER_POWER, SERIAL_ECHO_MSG("InlinePwr:",current_block->laser.power));
  2145. cutter.apply_power(current_block->laser.status.isPowered ? current_block->laser.power : 0);
  2146. #endif
  2147. }
  2148. }
  2149. #endif // LASER_FEATURE
  2150. // If the endstop is already pressed, endstop interrupts won't invoke
  2151. // endstop_triggered and the move will grind. So check here for a
  2152. // triggered endstop, which marks the block for discard on the next ISR.
  2153. endstops.update();
  2154. #if ENABLED(Z_LATE_ENABLE)
  2155. // If delayed Z enable, enable it now. This option will severely interfere with
  2156. // timing between pulses when chaining motion between blocks, and it could lead
  2157. // to lost steps in both X and Y axis, so avoid using it unless strictly necessary!!
  2158. if (current_block->steps.z) enable_axis(Z_AXIS);
  2159. #endif
  2160. // Mark the time_nominal as not calculated yet
  2161. ticks_nominal = -1;
  2162. #if ENABLED(S_CURVE_ACCELERATION)
  2163. // Initialize the Bézier speed curve
  2164. _calc_bezier_curve_coeffs(current_block->initial_rate, current_block->cruise_rate, current_block->acceleration_time_inverse);
  2165. // We haven't started the 2nd half of the trapezoid
  2166. bezier_2nd_half = false;
  2167. #else
  2168. // Set as deceleration point the initial rate of the block
  2169. acc_step_rate = current_block->initial_rate;
  2170. #endif
  2171. // Calculate the initial timer interval
  2172. interval = calc_timer_interval(current_block->initial_rate << oversampling_factor, steps_per_isr);
  2173. acceleration_time += interval;
  2174. #if ENABLED(LIN_ADVANCE)
  2175. if (current_block->la_advance_rate) {
  2176. const uint32_t la_step_rate = la_advance_steps < current_block->max_adv_steps ? current_block->la_advance_rate : 0;
  2177. la_interval = calc_timer_interval(current_block->initial_rate + la_step_rate) << current_block->la_scaling;
  2178. }
  2179. #endif
  2180. }
  2181. }
  2182. // Return the interval to wait
  2183. return interval;
  2184. }
  2185. #if ENABLED(LIN_ADVANCE)
  2186. // Timer interrupt for E. LA_steps is set in the main routine
  2187. void Stepper::advance_isr() {
  2188. // Apply Bresenham algorithm so that linear advance can piggy back on
  2189. // the acceleration and speed values calculated in block_phase_isr().
  2190. // This helps keep LA in sync with, for example, S_CURVE_ACCELERATION.
  2191. la_delta_error += la_dividend;
  2192. if (la_delta_error >= 0) {
  2193. count_position.e += count_direction.e;
  2194. la_advance_steps += count_direction.e;
  2195. la_delta_error -= advance_divisor;
  2196. // Set the STEP pulse ON
  2197. #if ENABLED(MIXING_EXTRUDER)
  2198. E_STEP_WRITE(mixer.get_next_stepper(), !INVERT_E_STEP_PIN);
  2199. #else
  2200. E_STEP_WRITE(stepper_extruder, !INVERT_E_STEP_PIN);
  2201. #endif
  2202. // Enforce a minimum duration for STEP pulse ON
  2203. #if ISR_PULSE_CONTROL
  2204. USING_TIMED_PULSE();
  2205. START_HIGH_PULSE();
  2206. AWAIT_HIGH_PULSE();
  2207. #endif
  2208. // Set the STEP pulse OFF
  2209. #if ENABLED(MIXING_EXTRUDER)
  2210. E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
  2211. #else
  2212. E_STEP_WRITE(stepper_extruder, INVERT_E_STEP_PIN);
  2213. #endif
  2214. }
  2215. }
  2216. #endif // LIN_ADVANCE
  2217. #if ENABLED(INTEGRATED_BABYSTEPPING)
  2218. // Timer interrupt for baby-stepping
  2219. uint32_t Stepper::babystepping_isr() {
  2220. babystep.task();
  2221. return babystep.has_steps() ? BABYSTEP_TICKS : BABYSTEP_NEVER;
  2222. }
  2223. #endif
  2224. // Check if the given block is busy or not - Must not be called from ISR contexts
  2225. // The current_block could change in the middle of the read by an Stepper ISR, so
  2226. // we must explicitly prevent that!
  2227. bool Stepper::is_block_busy(const block_t * const block) {
  2228. #ifdef __AVR__
  2229. // A SW memory barrier, to ensure GCC does not overoptimize loops
  2230. #define sw_barrier() asm volatile("": : :"memory");
  2231. // Keep reading until 2 consecutive reads return the same value,
  2232. // meaning there was no update in-between caused by an interrupt.
  2233. // This works because stepper ISRs happen at a slower rate than
  2234. // successive reads of a variable, so 2 consecutive reads with
  2235. // the same value means no interrupt updated it.
  2236. block_t *vold, *vnew = current_block;
  2237. sw_barrier();
  2238. do {
  2239. vold = vnew;
  2240. vnew = current_block;
  2241. sw_barrier();
  2242. } while (vold != vnew);
  2243. #else
  2244. block_t *vnew = current_block;
  2245. #endif
  2246. // Return if the block is busy or not
  2247. return block == vnew;
  2248. }
  2249. void Stepper::init() {
  2250. #if MB(ALLIGATOR)
  2251. const float motor_current[] = MOTOR_CURRENT;
  2252. unsigned int digipot_motor = 0;
  2253. LOOP_L_N(i, 3 + EXTRUDERS) {
  2254. digipot_motor = 255 * (motor_current[i] / 2.5);
  2255. dac084s085::setValue(i, digipot_motor);
  2256. }
  2257. #endif
  2258. // Init Microstepping Pins
  2259. TERN_(HAS_MICROSTEPS, microstep_init());
  2260. // Init Dir Pins
  2261. TERN_(HAS_X_DIR, X_DIR_INIT());
  2262. TERN_(HAS_X2_DIR, X2_DIR_INIT());
  2263. #if HAS_Y_DIR
  2264. Y_DIR_INIT();
  2265. #if BOTH(HAS_DUAL_Y_STEPPERS, HAS_Y2_DIR)
  2266. Y2_DIR_INIT();
  2267. #endif
  2268. #endif
  2269. #if HAS_Z_DIR
  2270. Z_DIR_INIT();
  2271. #if NUM_Z_STEPPERS >= 2 && HAS_Z2_DIR
  2272. Z2_DIR_INIT();
  2273. #endif
  2274. #if NUM_Z_STEPPERS >= 3 && HAS_Z3_DIR
  2275. Z3_DIR_INIT();
  2276. #endif
  2277. #if NUM_Z_STEPPERS >= 4 && HAS_Z4_DIR
  2278. Z4_DIR_INIT();
  2279. #endif
  2280. #endif
  2281. #if HAS_I_DIR
  2282. I_DIR_INIT();
  2283. #endif
  2284. #if HAS_J_DIR
  2285. J_DIR_INIT();
  2286. #endif
  2287. #if HAS_K_DIR
  2288. K_DIR_INIT();
  2289. #endif
  2290. #if HAS_U_DIR
  2291. U_DIR_INIT();
  2292. #endif
  2293. #if HAS_V_DIR
  2294. V_DIR_INIT();
  2295. #endif
  2296. #if HAS_W_DIR
  2297. W_DIR_INIT();
  2298. #endif
  2299. #if HAS_E0_DIR
  2300. E0_DIR_INIT();
  2301. #endif
  2302. #if HAS_E1_DIR
  2303. E1_DIR_INIT();
  2304. #endif
  2305. #if HAS_E2_DIR
  2306. E2_DIR_INIT();
  2307. #endif
  2308. #if HAS_E3_DIR
  2309. E3_DIR_INIT();
  2310. #endif
  2311. #if HAS_E4_DIR
  2312. E4_DIR_INIT();
  2313. #endif
  2314. #if HAS_E5_DIR
  2315. E5_DIR_INIT();
  2316. #endif
  2317. #if HAS_E6_DIR
  2318. E6_DIR_INIT();
  2319. #endif
  2320. #if HAS_E7_DIR
  2321. E7_DIR_INIT();
  2322. #endif
  2323. // Init Enable Pins - steppers default to disabled.
  2324. #if HAS_X_ENABLE
  2325. X_ENABLE_INIT();
  2326. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  2327. #if BOTH(HAS_X2_STEPPER, HAS_X2_ENABLE)
  2328. X2_ENABLE_INIT();
  2329. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  2330. #endif
  2331. #endif
  2332. #if HAS_Y_ENABLE
  2333. Y_ENABLE_INIT();
  2334. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  2335. #if BOTH(HAS_DUAL_Y_STEPPERS, HAS_Y2_ENABLE)
  2336. Y2_ENABLE_INIT();
  2337. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  2338. #endif
  2339. #endif
  2340. #if HAS_Z_ENABLE
  2341. Z_ENABLE_INIT();
  2342. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  2343. #if NUM_Z_STEPPERS >= 2 && HAS_Z2_ENABLE
  2344. Z2_ENABLE_INIT();
  2345. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  2346. #endif
  2347. #if NUM_Z_STEPPERS >= 3 && HAS_Z3_ENABLE
  2348. Z3_ENABLE_INIT();
  2349. if (!Z_ENABLE_ON) Z3_ENABLE_WRITE(HIGH);
  2350. #endif
  2351. #if NUM_Z_STEPPERS >= 4 && HAS_Z4_ENABLE
  2352. Z4_ENABLE_INIT();
  2353. if (!Z_ENABLE_ON) Z4_ENABLE_WRITE(HIGH);
  2354. #endif
  2355. #endif
  2356. #if HAS_I_ENABLE
  2357. I_ENABLE_INIT();
  2358. if (!I_ENABLE_ON) I_ENABLE_WRITE(HIGH);
  2359. #endif
  2360. #if HAS_J_ENABLE
  2361. J_ENABLE_INIT();
  2362. if (!J_ENABLE_ON) J_ENABLE_WRITE(HIGH);
  2363. #endif
  2364. #if HAS_K_ENABLE
  2365. K_ENABLE_INIT();
  2366. if (!K_ENABLE_ON) K_ENABLE_WRITE(HIGH);
  2367. #endif
  2368. #if HAS_U_ENABLE
  2369. U_ENABLE_INIT();
  2370. if (!U_ENABLE_ON) U_ENABLE_WRITE(HIGH);
  2371. #endif
  2372. #if HAS_V_ENABLE
  2373. V_ENABLE_INIT();
  2374. if (!V_ENABLE_ON) V_ENABLE_WRITE(HIGH);
  2375. #endif
  2376. #if HAS_W_ENABLE
  2377. W_ENABLE_INIT();
  2378. if (!W_ENABLE_ON) W_ENABLE_WRITE(HIGH);
  2379. #endif
  2380. #if HAS_E0_ENABLE
  2381. E0_ENABLE_INIT();
  2382. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  2383. #endif
  2384. #if HAS_E1_ENABLE
  2385. E1_ENABLE_INIT();
  2386. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  2387. #endif
  2388. #if HAS_E2_ENABLE
  2389. E2_ENABLE_INIT();
  2390. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  2391. #endif
  2392. #if HAS_E3_ENABLE
  2393. E3_ENABLE_INIT();
  2394. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  2395. #endif
  2396. #if HAS_E4_ENABLE
  2397. E4_ENABLE_INIT();
  2398. if (!E_ENABLE_ON) E4_ENABLE_WRITE(HIGH);
  2399. #endif
  2400. #if HAS_E5_ENABLE
  2401. E5_ENABLE_INIT();
  2402. if (!E_ENABLE_ON) E5_ENABLE_WRITE(HIGH);
  2403. #endif
  2404. #if HAS_E6_ENABLE
  2405. E6_ENABLE_INIT();
  2406. if (!E_ENABLE_ON) E6_ENABLE_WRITE(HIGH);
  2407. #endif
  2408. #if HAS_E7_ENABLE
  2409. E7_ENABLE_INIT();
  2410. if (!E_ENABLE_ON) E7_ENABLE_WRITE(HIGH);
  2411. #endif
  2412. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT()
  2413. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  2414. #define _DISABLE_AXIS(AXIS) DISABLE_AXIS_## AXIS()
  2415. #define AXIS_INIT(AXIS, PIN) \
  2416. _STEP_INIT(AXIS); \
  2417. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  2418. _DISABLE_AXIS(AXIS)
  2419. #define E_AXIS_INIT(NUM) AXIS_INIT(E## NUM, E)
  2420. // Init Step Pins
  2421. #if HAS_X_STEP
  2422. #if HAS_X2_STEPPER
  2423. X2_STEP_INIT();
  2424. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  2425. #endif
  2426. AXIS_INIT(X, X);
  2427. #endif
  2428. #if HAS_Y_STEP
  2429. #if HAS_DUAL_Y_STEPPERS
  2430. Y2_STEP_INIT();
  2431. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  2432. #endif
  2433. AXIS_INIT(Y, Y);
  2434. #endif
  2435. #if HAS_Z_STEP
  2436. #if NUM_Z_STEPPERS >= 2
  2437. Z2_STEP_INIT();
  2438. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  2439. #endif
  2440. #if NUM_Z_STEPPERS >= 3
  2441. Z3_STEP_INIT();
  2442. Z3_STEP_WRITE(INVERT_Z_STEP_PIN);
  2443. #endif
  2444. #if NUM_Z_STEPPERS >= 4
  2445. Z4_STEP_INIT();
  2446. Z4_STEP_WRITE(INVERT_Z_STEP_PIN);
  2447. #endif
  2448. AXIS_INIT(Z, Z);
  2449. #endif
  2450. #if HAS_I_STEP
  2451. AXIS_INIT(I, I);
  2452. #endif
  2453. #if HAS_J_STEP
  2454. AXIS_INIT(J, J);
  2455. #endif
  2456. #if HAS_K_STEP
  2457. AXIS_INIT(K, K);
  2458. #endif
  2459. #if HAS_U_STEP
  2460. AXIS_INIT(U, U);
  2461. #endif
  2462. #if HAS_V_STEP
  2463. AXIS_INIT(V, V);
  2464. #endif
  2465. #if HAS_W_STEP
  2466. AXIS_INIT(W, W);
  2467. #endif
  2468. #if E_STEPPERS && HAS_E0_STEP
  2469. E_AXIS_INIT(0);
  2470. #endif
  2471. #if (E_STEPPERS > 1 || ENABLED(E_DUAL_STEPPER_DRIVERS)) && HAS_E1_STEP
  2472. E_AXIS_INIT(1);
  2473. #endif
  2474. #if E_STEPPERS > 2 && HAS_E2_STEP
  2475. E_AXIS_INIT(2);
  2476. #endif
  2477. #if E_STEPPERS > 3 && HAS_E3_STEP
  2478. E_AXIS_INIT(3);
  2479. #endif
  2480. #if E_STEPPERS > 4 && HAS_E4_STEP
  2481. E_AXIS_INIT(4);
  2482. #endif
  2483. #if E_STEPPERS > 5 && HAS_E5_STEP
  2484. E_AXIS_INIT(5);
  2485. #endif
  2486. #if E_STEPPERS > 6 && HAS_E6_STEP
  2487. E_AXIS_INIT(6);
  2488. #endif
  2489. #if E_STEPPERS > 7 && HAS_E7_STEP
  2490. E_AXIS_INIT(7);
  2491. #endif
  2492. #if DISABLED(I2S_STEPPER_STREAM)
  2493. HAL_timer_start(MF_TIMER_STEP, 122); // Init Stepper ISR to 122 Hz for quick starting
  2494. wake_up();
  2495. sei();
  2496. #endif
  2497. // Init direction bits for first moves
  2498. set_directions(0
  2499. NUM_AXIS_GANG(
  2500. | TERN0(INVERT_X_DIR, _BV(X_AXIS)),
  2501. | TERN0(INVERT_Y_DIR, _BV(Y_AXIS)),
  2502. | TERN0(INVERT_Z_DIR, _BV(Z_AXIS)),
  2503. | TERN0(INVERT_I_DIR, _BV(I_AXIS)),
  2504. | TERN0(INVERT_J_DIR, _BV(J_AXIS)),
  2505. | TERN0(INVERT_K_DIR, _BV(K_AXIS)),
  2506. | TERN0(INVERT_U_DIR, _BV(U_AXIS)),
  2507. | TERN0(INVERT_V_DIR, _BV(V_AXIS)),
  2508. | TERN0(INVERT_W_DIR, _BV(W_AXIS))
  2509. )
  2510. );
  2511. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  2512. initialized = true;
  2513. digipot_init();
  2514. #endif
  2515. }
  2516. /**
  2517. * Set the stepper positions directly in steps
  2518. *
  2519. * The input is based on the typical per-axis XYZE steps.
  2520. * For CORE machines XYZ needs to be translated to ABC.
  2521. *
  2522. * This allows get_axis_position_mm to correctly
  2523. * derive the current XYZE position later on.
  2524. */
  2525. void Stepper::_set_position(const abce_long_t &spos) {
  2526. #if ANY(IS_CORE, MARKFORGED_XY, MARKFORGED_YX)
  2527. #if CORE_IS_XY
  2528. // corexy positioning
  2529. // these equations follow the form of the dA and dB equations on https://www.corexy.com/theory.html
  2530. count_position.set(spos.a + spos.b, CORESIGN(spos.a - spos.b), spos.c);
  2531. #elif CORE_IS_XZ
  2532. // corexz planning
  2533. count_position.set(spos.a + spos.c, spos.b, CORESIGN(spos.a - spos.c));
  2534. #elif CORE_IS_YZ
  2535. // coreyz planning
  2536. count_position.set(spos.a, spos.b + spos.c, CORESIGN(spos.b - spos.c));
  2537. #elif ENABLED(MARKFORGED_XY)
  2538. count_position.set(spos.a - spos.b, spos.b, spos.c);
  2539. #elif ENABLED(MARKFORGED_YX)
  2540. count_position.set(spos.a, spos.b - spos.a, spos.c);
  2541. #endif
  2542. SECONDARY_AXIS_CODE(
  2543. count_position.i = spos.i,
  2544. count_position.j = spos.j,
  2545. count_position.k = spos.k,
  2546. count_position.u = spos.u,
  2547. count_position.v = spos.v,
  2548. count_position.w = spos.w
  2549. );
  2550. TERN_(HAS_EXTRUDERS, count_position.e = spos.e);
  2551. #else
  2552. // default non-h-bot planning
  2553. count_position = spos;
  2554. #endif
  2555. }
  2556. /**
  2557. * Get a stepper's position in steps.
  2558. */
  2559. int32_t Stepper::position(const AxisEnum axis) {
  2560. #ifdef __AVR__
  2561. // Protect the access to the position. Only required for AVR, as
  2562. // any 32bit CPU offers atomic access to 32bit variables
  2563. const bool was_enabled = suspend();
  2564. #endif
  2565. const int32_t v = count_position[axis];
  2566. #ifdef __AVR__
  2567. // Reenable Stepper ISR
  2568. if (was_enabled) wake_up();
  2569. #endif
  2570. return v;
  2571. }
  2572. // Set the current position in steps
  2573. void Stepper::set_position(const xyze_long_t &spos) {
  2574. planner.synchronize();
  2575. const bool was_enabled = suspend();
  2576. _set_position(spos);
  2577. if (was_enabled) wake_up();
  2578. }
  2579. void Stepper::set_axis_position(const AxisEnum a, const int32_t &v) {
  2580. planner.synchronize();
  2581. #ifdef __AVR__
  2582. // Protect the access to the position. Only required for AVR, as
  2583. // any 32bit CPU offers atomic access to 32bit variables
  2584. const bool was_enabled = suspend();
  2585. #endif
  2586. count_position[a] = v;
  2587. #ifdef __AVR__
  2588. // Reenable Stepper ISR
  2589. if (was_enabled) wake_up();
  2590. #endif
  2591. }
  2592. // Signal endstops were triggered - This function can be called from
  2593. // an ISR context (Temperature, Stepper or limits ISR), so we must
  2594. // be very careful here. If the interrupt being preempted was the
  2595. // Stepper ISR (this CAN happen with the endstop limits ISR) then
  2596. // when the stepper ISR resumes, we must be very sure that the movement
  2597. // is properly canceled
  2598. void Stepper::endstop_triggered(const AxisEnum axis) {
  2599. const bool was_enabled = suspend();
  2600. endstops_trigsteps[axis] = (
  2601. #if IS_CORE
  2602. (axis == CORE_AXIS_2
  2603. ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  2604. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  2605. ) * double(0.5)
  2606. #elif ENABLED(MARKFORGED_XY)
  2607. axis == CORE_AXIS_1
  2608. ? count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2]
  2609. : count_position[CORE_AXIS_2]
  2610. #elif ENABLED(MARKFORGED_YX)
  2611. axis == CORE_AXIS_1
  2612. ? count_position[CORE_AXIS_1]
  2613. : count_position[CORE_AXIS_2] - count_position[CORE_AXIS_1]
  2614. #else // !IS_CORE
  2615. count_position[axis]
  2616. #endif
  2617. );
  2618. // Discard the rest of the move if there is a current block
  2619. quick_stop();
  2620. if (was_enabled) wake_up();
  2621. }
  2622. int32_t Stepper::triggered_position(const AxisEnum axis) {
  2623. #ifdef __AVR__
  2624. // Protect the access to the position. Only required for AVR, as
  2625. // any 32bit CPU offers atomic access to 32bit variables
  2626. const bool was_enabled = suspend();
  2627. #endif
  2628. const int32_t v = endstops_trigsteps[axis];
  2629. #ifdef __AVR__
  2630. // Reenable Stepper ISR
  2631. if (was_enabled) wake_up();
  2632. #endif
  2633. return v;
  2634. }
  2635. #if ANY(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY, MARKFORGED_YX, IS_SCARA, DELTA)
  2636. #define SAYS_A 1
  2637. #endif
  2638. #if ANY(CORE_IS_XY, CORE_IS_YZ, MARKFORGED_XY, MARKFORGED_YX, IS_SCARA, DELTA)
  2639. #define SAYS_B 1
  2640. #endif
  2641. #if ANY(CORE_IS_XZ, CORE_IS_YZ, DELTA)
  2642. #define SAYS_C 1
  2643. #endif
  2644. void Stepper::report_a_position(const xyz_long_t &pos) {
  2645. SERIAL_ECHOLNPGM_P(
  2646. LIST_N(DOUBLE(NUM_AXES),
  2647. TERN(SAYS_A, PSTR(STR_COUNT_A), PSTR(STR_COUNT_X)), pos.x,
  2648. TERN(SAYS_B, PSTR("B:"), SP_Y_LBL), pos.y,
  2649. TERN(SAYS_C, PSTR("C:"), SP_Z_LBL), pos.z,
  2650. SP_I_LBL, pos.i,
  2651. SP_J_LBL, pos.j,
  2652. SP_K_LBL, pos.k,
  2653. SP_U_LBL, pos.u,
  2654. SP_V_LBL, pos.v,
  2655. SP_W_LBL, pos.w
  2656. )
  2657. );
  2658. }
  2659. void Stepper::report_positions() {
  2660. #ifdef __AVR__
  2661. // Protect the access to the position.
  2662. const bool was_enabled = suspend();
  2663. #endif
  2664. const xyz_long_t pos = count_position;
  2665. #ifdef __AVR__
  2666. if (was_enabled) wake_up();
  2667. #endif
  2668. report_a_position(pos);
  2669. }
  2670. #if ENABLED(BABYSTEPPING)
  2671. #define _ENABLE_AXIS(A) enable_axis(_AXIS(A))
  2672. #define _READ_DIR(AXIS) AXIS ##_DIR_READ()
  2673. #define _INVERT_DIR(AXIS) ENABLED(INVERT_## AXIS ##_DIR)
  2674. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  2675. #if MINIMUM_STEPPER_PULSE
  2676. #define STEP_PULSE_CYCLES ((MINIMUM_STEPPER_PULSE) * CYCLES_PER_MICROSECOND)
  2677. #else
  2678. #define STEP_PULSE_CYCLES 0
  2679. #endif
  2680. #if ENABLED(DELTA)
  2681. #define CYCLES_EATEN_BABYSTEP (2 * 15)
  2682. #else
  2683. #define CYCLES_EATEN_BABYSTEP 0
  2684. #endif
  2685. #if CYCLES_EATEN_BABYSTEP < STEP_PULSE_CYCLES
  2686. #define EXTRA_CYCLES_BABYSTEP (STEP_PULSE_CYCLES - (CYCLES_EATEN_BABYSTEP))
  2687. #else
  2688. #define EXTRA_CYCLES_BABYSTEP 0
  2689. #endif
  2690. #if EXTRA_CYCLES_BABYSTEP > 20
  2691. #define _SAVE_START() const hal_timer_t pulse_start = HAL_timer_get_count(MF_TIMER_PULSE)
  2692. #define _PULSE_WAIT() while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_count(MF_TIMER_PULSE) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  2693. #else
  2694. #define _SAVE_START() NOOP
  2695. #if EXTRA_CYCLES_BABYSTEP > 0
  2696. #define _PULSE_WAIT() DELAY_NS(EXTRA_CYCLES_BABYSTEP * NANOSECONDS_PER_CYCLE)
  2697. #elif ENABLED(DELTA)
  2698. #define _PULSE_WAIT() DELAY_US(2);
  2699. #elif STEP_PULSE_CYCLES > 0
  2700. #define _PULSE_WAIT() NOOP
  2701. #else
  2702. #define _PULSE_WAIT() DELAY_US(4);
  2703. #endif
  2704. #endif
  2705. #if ENABLED(BABYSTEPPING_EXTRA_DIR_WAIT)
  2706. #define EXTRA_DIR_WAIT_BEFORE DIR_WAIT_BEFORE
  2707. #define EXTRA_DIR_WAIT_AFTER DIR_WAIT_AFTER
  2708. #else
  2709. #define EXTRA_DIR_WAIT_BEFORE()
  2710. #define EXTRA_DIR_WAIT_AFTER()
  2711. #endif
  2712. #if DISABLED(DELTA)
  2713. #define BABYSTEP_AXIS(AXIS, INV, DIR) do{ \
  2714. const uint8_t old_dir = _READ_DIR(AXIS); \
  2715. _ENABLE_AXIS(AXIS); \
  2716. DIR_WAIT_BEFORE(); \
  2717. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^DIR^INV); \
  2718. DIR_WAIT_AFTER(); \
  2719. _SAVE_START(); \
  2720. _APPLY_STEP(AXIS, !_INVERT_STEP_PIN(AXIS), true); \
  2721. _PULSE_WAIT(); \
  2722. _APPLY_STEP(AXIS, _INVERT_STEP_PIN(AXIS), true); \
  2723. EXTRA_DIR_WAIT_BEFORE(); \
  2724. _APPLY_DIR(AXIS, old_dir); \
  2725. EXTRA_DIR_WAIT_AFTER(); \
  2726. }while(0)
  2727. #endif
  2728. #if IS_CORE
  2729. #define BABYSTEP_CORE(A, B, INV, DIR, ALT) do{ \
  2730. const xy_byte_t old_dir = { _READ_DIR(A), _READ_DIR(B) }; \
  2731. _ENABLE_AXIS(A); _ENABLE_AXIS(B); \
  2732. DIR_WAIT_BEFORE(); \
  2733. _APPLY_DIR(A, _INVERT_DIR(A)^DIR^INV); \
  2734. _APPLY_DIR(B, _INVERT_DIR(B)^DIR^INV^ALT); \
  2735. DIR_WAIT_AFTER(); \
  2736. _SAVE_START(); \
  2737. _APPLY_STEP(A, !_INVERT_STEP_PIN(A), true); \
  2738. _APPLY_STEP(B, !_INVERT_STEP_PIN(B), true); \
  2739. _PULSE_WAIT(); \
  2740. _APPLY_STEP(A, _INVERT_STEP_PIN(A), true); \
  2741. _APPLY_STEP(B, _INVERT_STEP_PIN(B), true); \
  2742. EXTRA_DIR_WAIT_BEFORE(); \
  2743. _APPLY_DIR(A, old_dir.a); _APPLY_DIR(B, old_dir.b); \
  2744. EXTRA_DIR_WAIT_AFTER(); \
  2745. }while(0)
  2746. #endif
  2747. // MUST ONLY BE CALLED BY AN ISR,
  2748. // No other ISR should ever interrupt this!
  2749. void Stepper::do_babystep(const AxisEnum axis, const bool direction) {
  2750. IF_DISABLED(INTEGRATED_BABYSTEPPING, cli());
  2751. switch (axis) {
  2752. #if ENABLED(BABYSTEP_XY)
  2753. case X_AXIS:
  2754. #if CORE_IS_XY
  2755. BABYSTEP_CORE(X, Y, 0, direction, 0);
  2756. #elif CORE_IS_XZ
  2757. BABYSTEP_CORE(X, Z, 0, direction, 0);
  2758. #else
  2759. BABYSTEP_AXIS(X, 0, direction);
  2760. #endif
  2761. break;
  2762. case Y_AXIS:
  2763. #if CORE_IS_XY
  2764. BABYSTEP_CORE(X, Y, 1, !direction, (CORESIGN(1)>0));
  2765. #elif CORE_IS_YZ
  2766. BABYSTEP_CORE(Y, Z, 0, direction, (CORESIGN(1)<0));
  2767. #else
  2768. BABYSTEP_AXIS(Y, 0, direction);
  2769. #endif
  2770. break;
  2771. #endif
  2772. case Z_AXIS: {
  2773. #if CORE_IS_XZ
  2774. BABYSTEP_CORE(X, Z, BABYSTEP_INVERT_Z, direction, (CORESIGN(1)<0));
  2775. #elif CORE_IS_YZ
  2776. BABYSTEP_CORE(Y, Z, BABYSTEP_INVERT_Z, direction, (CORESIGN(1)<0));
  2777. #elif DISABLED(DELTA)
  2778. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction);
  2779. #else // DELTA
  2780. const bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  2781. NUM_AXIS_CODE(
  2782. enable_axis(X_AXIS), enable_axis(Y_AXIS), enable_axis(Z_AXIS),
  2783. enable_axis(I_AXIS), enable_axis(J_AXIS), enable_axis(K_AXIS),
  2784. enable_axis(U_AXIS), enable_axis(V_AXIS), enable_axis(W_AXIS)
  2785. );
  2786. DIR_WAIT_BEFORE();
  2787. const xyz_byte_t old_dir = NUM_AXIS_ARRAY(
  2788. X_DIR_READ(), Y_DIR_READ(), Z_DIR_READ(),
  2789. I_DIR_READ(), J_DIR_READ(), K_DIR_READ(),
  2790. U_DIR_READ(), V_DIR_READ(), W_DIR_READ()
  2791. );
  2792. X_DIR_WRITE(ENABLED(INVERT_X_DIR) ^ z_direction);
  2793. #ifdef Y_DIR_WRITE
  2794. Y_DIR_WRITE(ENABLED(INVERT_Y_DIR) ^ z_direction);
  2795. #endif
  2796. #ifdef Z_DIR_WRITE
  2797. Z_DIR_WRITE(ENABLED(INVERT_Z_DIR) ^ z_direction);
  2798. #endif
  2799. #ifdef I_DIR_WRITE
  2800. I_DIR_WRITE(ENABLED(INVERT_I_DIR) ^ z_direction);
  2801. #endif
  2802. #ifdef J_DIR_WRITE
  2803. J_DIR_WRITE(ENABLED(INVERT_J_DIR) ^ z_direction);
  2804. #endif
  2805. #ifdef K_DIR_WRITE
  2806. K_DIR_WRITE(ENABLED(INVERT_K_DIR) ^ z_direction);
  2807. #endif
  2808. #ifdef U_DIR_WRITE
  2809. U_DIR_WRITE(ENABLED(INVERT_U_DIR) ^ z_direction);
  2810. #endif
  2811. #ifdef V_DIR_WRITE
  2812. V_DIR_WRITE(ENABLED(INVERT_V_DIR) ^ z_direction);
  2813. #endif
  2814. #ifdef W_DIR_WRITE
  2815. W_DIR_WRITE(ENABLED(INVERT_W_DIR) ^ z_direction);
  2816. #endif
  2817. DIR_WAIT_AFTER();
  2818. _SAVE_START();
  2819. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  2820. #ifdef Y_STEP_WRITE
  2821. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  2822. #endif
  2823. #ifdef Z_STEP_WRITE
  2824. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  2825. #endif
  2826. #ifdef I_STEP_WRITE
  2827. I_STEP_WRITE(!INVERT_I_STEP_PIN);
  2828. #endif
  2829. #ifdef J_STEP_WRITE
  2830. J_STEP_WRITE(!INVERT_J_STEP_PIN);
  2831. #endif
  2832. #ifdef K_STEP_WRITE
  2833. K_STEP_WRITE(!INVERT_K_STEP_PIN);
  2834. #endif
  2835. #ifdef U_STEP_WRITE
  2836. U_STEP_WRITE(!INVERT_U_STEP_PIN);
  2837. #endif
  2838. #ifdef V_STEP_WRITE
  2839. V_STEP_WRITE(!INVERT_V_STEP_PIN);
  2840. #endif
  2841. #ifdef W_STEP_WRITE
  2842. W_STEP_WRITE(!INVERT_W_STEP_PIN);
  2843. #endif
  2844. _PULSE_WAIT();
  2845. X_STEP_WRITE(INVERT_X_STEP_PIN);
  2846. #ifdef Y_STEP_WRITE
  2847. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  2848. #endif
  2849. #ifdef Z_STEP_WRITE
  2850. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  2851. #endif
  2852. #ifdef I_STEP_WRITE
  2853. I_STEP_WRITE(INVERT_I_STEP_PIN);
  2854. #endif
  2855. #ifdef J_STEP_WRITE
  2856. J_STEP_WRITE(INVERT_J_STEP_PIN);
  2857. #endif
  2858. #ifdef K_STEP_WRITE
  2859. K_STEP_WRITE(INVERT_K_STEP_PIN);
  2860. #endif
  2861. #ifdef U_STEP_WRITE
  2862. U_STEP_WRITE(INVERT_U_STEP_PIN);
  2863. #endif
  2864. #ifdef V_STEP_WRITE
  2865. V_STEP_WRITE(INVERT_V_STEP_PIN);
  2866. #endif
  2867. #ifdef W_STEP_WRITE
  2868. W_STEP_WRITE(INVERT_W_STEP_PIN);
  2869. #endif
  2870. // Restore direction bits
  2871. EXTRA_DIR_WAIT_BEFORE();
  2872. X_DIR_WRITE(old_dir.x);
  2873. #ifdef Y_DIR_WRITE
  2874. Y_DIR_WRITE(old_dir.y);
  2875. #endif
  2876. #ifdef Z_DIR_WRITE
  2877. Z_DIR_WRITE(old_dir.z);
  2878. #endif
  2879. #ifdef I_DIR_WRITE
  2880. I_DIR_WRITE(old_dir.i);
  2881. #endif
  2882. #ifdef J_DIR_WRITE
  2883. J_DIR_WRITE(old_dir.j);
  2884. #endif
  2885. #ifdef K_DIR_WRITE
  2886. K_DIR_WRITE(old_dir.k);
  2887. #endif
  2888. #ifdef U_DIR_WRITE
  2889. U_DIR_WRITE(old_dir.u);
  2890. #endif
  2891. #ifdef V_DIR_WRITE
  2892. V_DIR_WRITE(old_dir.v);
  2893. #endif
  2894. #ifdef W_DIR_WRITE
  2895. W_DIR_WRITE(old_dir.w);
  2896. #endif
  2897. EXTRA_DIR_WAIT_AFTER();
  2898. #endif
  2899. } break;
  2900. #if HAS_I_AXIS
  2901. case I_AXIS: BABYSTEP_AXIS(I, 0, direction); break;
  2902. #endif
  2903. #if HAS_J_AXIS
  2904. case J_AXIS: BABYSTEP_AXIS(J, 0, direction); break;
  2905. #endif
  2906. #if HAS_K_AXIS
  2907. case K_AXIS: BABYSTEP_AXIS(K, 0, direction); break;
  2908. #endif
  2909. #if HAS_U_AXIS
  2910. case U_AXIS: BABYSTEP_AXIS(U, 0, direction); break;
  2911. #endif
  2912. #if HAS_V_AXIS
  2913. case V_AXIS: BABYSTEP_AXIS(V, 0, direction); break;
  2914. #endif
  2915. #if HAS_W_AXIS
  2916. case W_AXIS: BABYSTEP_AXIS(W, 0, direction); break;
  2917. #endif
  2918. default: break;
  2919. }
  2920. IF_DISABLED(INTEGRATED_BABYSTEPPING, sei());
  2921. }
  2922. #endif // BABYSTEPPING
  2923. /**
  2924. * Software-controlled Stepper Motor Current
  2925. */
  2926. #if HAS_MOTOR_CURRENT_SPI
  2927. // From Arduino DigitalPotControl example
  2928. void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) {
  2929. WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
  2930. SPI.transfer(address); // Send the address and value via SPI
  2931. SPI.transfer(value);
  2932. WRITE(DIGIPOTSS_PIN, HIGH); // Take the SS pin high to de-select the chip
  2933. //delay(10);
  2934. }
  2935. #endif // HAS_MOTOR_CURRENT_SPI
  2936. #if HAS_MOTOR_CURRENT_PWM
  2937. void Stepper::refresh_motor_power() {
  2938. if (!initialized) return;
  2939. LOOP_L_N(i, COUNT(motor_current_setting)) {
  2940. switch (i) {
  2941. #if ANY_PIN(MOTOR_CURRENT_PWM_XY, MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_I, MOTOR_CURRENT_PWM_J, MOTOR_CURRENT_PWM_K, MOTOR_CURRENT_PWM_U, MOTOR_CURRENT_PWM_V, MOTOR_CURRENT_PWM_W)
  2942. case 0:
  2943. #endif
  2944. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  2945. case 1:
  2946. #endif
  2947. #if ANY_PIN(MOTOR_CURRENT_PWM_E, MOTOR_CURRENT_PWM_E0, MOTOR_CURRENT_PWM_E1)
  2948. case 2:
  2949. #endif
  2950. set_digipot_current(i, motor_current_setting[i]);
  2951. default: break;
  2952. }
  2953. }
  2954. }
  2955. #endif // HAS_MOTOR_CURRENT_PWM
  2956. #if !MB(PRINTRBOARD_G2)
  2957. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  2958. void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) {
  2959. if (WITHIN(driver, 0, MOTOR_CURRENT_COUNT - 1))
  2960. motor_current_setting[driver] = current; // update motor_current_setting
  2961. if (!initialized) return;
  2962. #if HAS_MOTOR_CURRENT_SPI
  2963. //SERIAL_ECHOLNPGM("Digipotss current ", current);
  2964. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  2965. set_digipot_value_spi(digipot_ch[driver], current);
  2966. #elif HAS_MOTOR_CURRENT_PWM
  2967. #define _WRITE_CURRENT_PWM(P) hal.set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
  2968. switch (driver) {
  2969. case 0:
  2970. #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
  2971. _WRITE_CURRENT_PWM(X);
  2972. #endif
  2973. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
  2974. _WRITE_CURRENT_PWM(Y);
  2975. #endif
  2976. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  2977. _WRITE_CURRENT_PWM(XY);
  2978. #endif
  2979. #if PIN_EXISTS(MOTOR_CURRENT_PWM_I)
  2980. _WRITE_CURRENT_PWM(I);
  2981. #endif
  2982. #if PIN_EXISTS(MOTOR_CURRENT_PWM_J)
  2983. _WRITE_CURRENT_PWM(J);
  2984. #endif
  2985. #if PIN_EXISTS(MOTOR_CURRENT_PWM_K)
  2986. _WRITE_CURRENT_PWM(K);
  2987. #endif
  2988. #if PIN_EXISTS(MOTOR_CURRENT_PWM_U)
  2989. _WRITE_CURRENT_PWM(U);
  2990. #endif
  2991. #if PIN_EXISTS(MOTOR_CURRENT_PWM_V)
  2992. _WRITE_CURRENT_PWM(V);
  2993. #endif
  2994. #if PIN_EXISTS(MOTOR_CURRENT_PWM_W)
  2995. _WRITE_CURRENT_PWM(W);
  2996. #endif
  2997. break;
  2998. case 1:
  2999. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  3000. _WRITE_CURRENT_PWM(Z);
  3001. #endif
  3002. break;
  3003. case 2:
  3004. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  3005. _WRITE_CURRENT_PWM(E);
  3006. #endif
  3007. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E0)
  3008. _WRITE_CURRENT_PWM(E0);
  3009. #endif
  3010. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E1)
  3011. _WRITE_CURRENT_PWM(E1);
  3012. #endif
  3013. break;
  3014. }
  3015. #endif
  3016. }
  3017. void Stepper::digipot_init() {
  3018. #if HAS_MOTOR_CURRENT_SPI
  3019. SPI.begin();
  3020. SET_OUTPUT(DIGIPOTSS_PIN);
  3021. LOOP_L_N(i, COUNT(motor_current_setting))
  3022. set_digipot_current(i, motor_current_setting[i]);
  3023. #elif HAS_MOTOR_CURRENT_PWM
  3024. #ifdef __SAM3X8E__
  3025. #define _RESET_CURRENT_PWM_FREQ(P) NOOP
  3026. #else
  3027. #define _RESET_CURRENT_PWM_FREQ(P) hal.set_pwm_frequency(pin_t(P), MOTOR_CURRENT_PWM_FREQUENCY)
  3028. #endif
  3029. #define INIT_CURRENT_PWM(P) do{ SET_PWM(MOTOR_CURRENT_PWM_## P ##_PIN); _RESET_CURRENT_PWM_FREQ(MOTOR_CURRENT_PWM_## P ##_PIN); }while(0)
  3030. #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
  3031. INIT_CURRENT_PWM(X);
  3032. #endif
  3033. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
  3034. INIT_CURRENT_PWM(Y);
  3035. #endif
  3036. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  3037. INIT_CURRENT_PWM(XY);
  3038. #endif
  3039. #if PIN_EXISTS(MOTOR_CURRENT_PWM_I)
  3040. INIT_CURRENT_PWM(I);
  3041. #endif
  3042. #if PIN_EXISTS(MOTOR_CURRENT_PWM_J)
  3043. INIT_CURRENT_PWM(J);
  3044. #endif
  3045. #if PIN_EXISTS(MOTOR_CURRENT_PWM_K)
  3046. INIT_CURRENT_PWM(K);
  3047. #endif
  3048. #if PIN_EXISTS(MOTOR_CURRENT_PWM_U)
  3049. INIT_CURRENT_PWM(U);
  3050. #endif
  3051. #if PIN_EXISTS(MOTOR_CURRENT_PWM_V)
  3052. INIT_CURRENT_PWM(V);
  3053. #endif
  3054. #if PIN_EXISTS(MOTOR_CURRENT_PWM_W)
  3055. INIT_CURRENT_PWM(W);
  3056. #endif
  3057. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  3058. INIT_CURRENT_PWM(Z);
  3059. #endif
  3060. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  3061. INIT_CURRENT_PWM(E);
  3062. #endif
  3063. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E0)
  3064. INIT_CURRENT_PWM(E0);
  3065. #endif
  3066. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E1)
  3067. INIT_CURRENT_PWM(E1);
  3068. #endif
  3069. refresh_motor_power();
  3070. #endif
  3071. }
  3072. #endif
  3073. #else // PRINTRBOARD_G2
  3074. #include HAL_PATH(../HAL, fastio/G2_PWM.h)
  3075. #endif
  3076. #if HAS_MICROSTEPS
  3077. /**
  3078. * Software-controlled Microstepping
  3079. */
  3080. void Stepper::microstep_init() {
  3081. #if HAS_X_MS_PINS
  3082. SET_OUTPUT(X_MS1_PIN); SET_OUTPUT(X_MS2_PIN);
  3083. #if PIN_EXISTS(X_MS3)
  3084. SET_OUTPUT(X_MS3_PIN);
  3085. #endif
  3086. #endif
  3087. #if HAS_X2_MS_PINS
  3088. SET_OUTPUT(X2_MS1_PIN); SET_OUTPUT(X2_MS2_PIN);
  3089. #if PIN_EXISTS(X2_MS3)
  3090. SET_OUTPUT(X2_MS3_PIN);
  3091. #endif
  3092. #endif
  3093. #if HAS_Y_MS_PINS
  3094. SET_OUTPUT(Y_MS1_PIN); SET_OUTPUT(Y_MS2_PIN);
  3095. #if PIN_EXISTS(Y_MS3)
  3096. SET_OUTPUT(Y_MS3_PIN);
  3097. #endif
  3098. #endif
  3099. #if HAS_Y2_MS_PINS
  3100. SET_OUTPUT(Y2_MS1_PIN); SET_OUTPUT(Y2_MS2_PIN);
  3101. #if PIN_EXISTS(Y2_MS3)
  3102. SET_OUTPUT(Y2_MS3_PIN);
  3103. #endif
  3104. #endif
  3105. #if HAS_Z_MS_PINS
  3106. SET_OUTPUT(Z_MS1_PIN); SET_OUTPUT(Z_MS2_PIN);
  3107. #if PIN_EXISTS(Z_MS3)
  3108. SET_OUTPUT(Z_MS3_PIN);
  3109. #endif
  3110. #endif
  3111. #if HAS_Z2_MS_PINS
  3112. SET_OUTPUT(Z2_MS1_PIN); SET_OUTPUT(Z2_MS2_PIN);
  3113. #if PIN_EXISTS(Z2_MS3)
  3114. SET_OUTPUT(Z2_MS3_PIN);
  3115. #endif
  3116. #endif
  3117. #if HAS_Z3_MS_PINS
  3118. SET_OUTPUT(Z3_MS1_PIN); SET_OUTPUT(Z3_MS2_PIN);
  3119. #if PIN_EXISTS(Z3_MS3)
  3120. SET_OUTPUT(Z3_MS3_PIN);
  3121. #endif
  3122. #endif
  3123. #if HAS_Z4_MS_PINS
  3124. SET_OUTPUT(Z4_MS1_PIN); SET_OUTPUT(Z4_MS2_PIN);
  3125. #if PIN_EXISTS(Z4_MS3)
  3126. SET_OUTPUT(Z4_MS3_PIN);
  3127. #endif
  3128. #endif
  3129. #if HAS_I_MS_PINS
  3130. SET_OUTPUT(I_MS1_PIN); SET_OUTPUT(I_MS2_PIN);
  3131. #if PIN_EXISTS(I_MS3)
  3132. SET_OUTPUT(I_MS3_PIN);
  3133. #endif
  3134. #endif
  3135. #if HAS_J_MS_PINS
  3136. SET_OUTPUT(J_MS1_PIN); SET_OUTPUT(J_MS2_PIN);
  3137. #if PIN_EXISTS(J_MS3)
  3138. SET_OUTPUT(J_MS3_PIN);
  3139. #endif
  3140. #endif
  3141. #if HAS_K_MS_PINS
  3142. SET_OUTPUT(K_MS1_PIN); SET_OUTPUT(K_MS2_PIN);
  3143. #if PIN_EXISTS(K_MS3)
  3144. SET_OUTPUT(K_MS3_PIN);
  3145. #endif
  3146. #endif
  3147. #if HAS_U_MS_PINS
  3148. SET_OUTPUT(U_MS1_PIN); SET_OUTPUT(U_MS2_PIN);
  3149. #if PIN_EXISTS(U_MS3)
  3150. SET_OUTPUT(U_MS3_PIN);
  3151. #endif
  3152. #endif
  3153. #if HAS_V_MS_PINS
  3154. SET_OUTPUT(V_MS1_PIN); SET_OUTPUT(V_MS2_PIN);
  3155. #if PIN_EXISTS(V_MS3)
  3156. SET_OUTPUT(V_MS3_PIN);
  3157. #endif
  3158. #endif
  3159. #if HAS_W_MS_PINS
  3160. SET_OUTPUT(W_MS1_PIN); SET_OUTPUT(W_MS2_PIN);
  3161. #if PIN_EXISTS(W_MS3)
  3162. SET_OUTPUT(W_MS3_PIN);
  3163. #endif
  3164. #endif
  3165. #if HAS_E0_MS_PINS
  3166. SET_OUTPUT(E0_MS1_PIN); SET_OUTPUT(E0_MS2_PIN);
  3167. #if PIN_EXISTS(E0_MS3)
  3168. SET_OUTPUT(E0_MS3_PIN);
  3169. #endif
  3170. #endif
  3171. #if HAS_E1_MS_PINS
  3172. SET_OUTPUT(E1_MS1_PIN); SET_OUTPUT(E1_MS2_PIN);
  3173. #if PIN_EXISTS(E1_MS3)
  3174. SET_OUTPUT(E1_MS3_PIN);
  3175. #endif
  3176. #endif
  3177. #if HAS_E2_MS_PINS
  3178. SET_OUTPUT(E2_MS1_PIN); SET_OUTPUT(E2_MS2_PIN);
  3179. #if PIN_EXISTS(E2_MS3)
  3180. SET_OUTPUT(E2_MS3_PIN);
  3181. #endif
  3182. #endif
  3183. #if HAS_E3_MS_PINS
  3184. SET_OUTPUT(E3_MS1_PIN); SET_OUTPUT(E3_MS2_PIN);
  3185. #if PIN_EXISTS(E3_MS3)
  3186. SET_OUTPUT(E3_MS3_PIN);
  3187. #endif
  3188. #endif
  3189. #if HAS_E4_MS_PINS
  3190. SET_OUTPUT(E4_MS1_PIN); SET_OUTPUT(E4_MS2_PIN);
  3191. #if PIN_EXISTS(E4_MS3)
  3192. SET_OUTPUT(E4_MS3_PIN);
  3193. #endif
  3194. #endif
  3195. #if HAS_E5_MS_PINS
  3196. SET_OUTPUT(E5_MS1_PIN); SET_OUTPUT(E5_MS2_PIN);
  3197. #if PIN_EXISTS(E5_MS3)
  3198. SET_OUTPUT(E5_MS3_PIN);
  3199. #endif
  3200. #endif
  3201. #if HAS_E6_MS_PINS
  3202. SET_OUTPUT(E6_MS1_PIN); SET_OUTPUT(E6_MS2_PIN);
  3203. #if PIN_EXISTS(E6_MS3)
  3204. SET_OUTPUT(E6_MS3_PIN);
  3205. #endif
  3206. #endif
  3207. #if HAS_E7_MS_PINS
  3208. SET_OUTPUT(E7_MS1_PIN); SET_OUTPUT(E7_MS2_PIN);
  3209. #if PIN_EXISTS(E7_MS3)
  3210. SET_OUTPUT(E7_MS3_PIN);
  3211. #endif
  3212. #endif
  3213. static const uint8_t microstep_modes[] = MICROSTEP_MODES;
  3214. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  3215. microstep_mode(i, microstep_modes[i]);
  3216. }
  3217. void Stepper::microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2, const int8_t ms3) {
  3218. if (ms1 >= 0) switch (driver) {
  3219. #if HAS_X_MS_PINS || HAS_X2_MS_PINS
  3220. case X_AXIS:
  3221. #if HAS_X_MS_PINS
  3222. WRITE(X_MS1_PIN, ms1);
  3223. #endif
  3224. #if HAS_X2_MS_PINS
  3225. WRITE(X2_MS1_PIN, ms1);
  3226. #endif
  3227. break;
  3228. #endif
  3229. #if HAS_Y_MS_PINS || HAS_Y2_MS_PINS
  3230. case Y_AXIS:
  3231. #if HAS_Y_MS_PINS
  3232. WRITE(Y_MS1_PIN, ms1);
  3233. #endif
  3234. #if HAS_Y2_MS_PINS
  3235. WRITE(Y2_MS1_PIN, ms1);
  3236. #endif
  3237. break;
  3238. #endif
  3239. #if HAS_SOME_Z_MS_PINS
  3240. case Z_AXIS:
  3241. #if HAS_Z_MS_PINS
  3242. WRITE(Z_MS1_PIN, ms1);
  3243. #endif
  3244. #if HAS_Z2_MS_PINS
  3245. WRITE(Z2_MS1_PIN, ms1);
  3246. #endif
  3247. #if HAS_Z3_MS_PINS
  3248. WRITE(Z3_MS1_PIN, ms1);
  3249. #endif
  3250. #if HAS_Z4_MS_PINS
  3251. WRITE(Z4_MS1_PIN, ms1);
  3252. #endif
  3253. break;
  3254. #endif
  3255. #if HAS_I_MS_PINS
  3256. case I_AXIS: WRITE(I_MS1_PIN, ms1); break
  3257. #endif
  3258. #if HAS_J_MS_PINS
  3259. case J_AXIS: WRITE(J_MS1_PIN, ms1); break
  3260. #endif
  3261. #if HAS_K_MS_PINS
  3262. case K_AXIS: WRITE(K_MS1_PIN, ms1); break
  3263. #endif
  3264. #if HAS_U_MS_PINS
  3265. case U_AXIS: WRITE(U_MS1_PIN, ms1); break
  3266. #endif
  3267. #if HAS_V_MS_PINS
  3268. case V_AXIS: WRITE(V_MS1_PIN, ms1); break
  3269. #endif
  3270. #if HAS_W_MS_PINS
  3271. case W_AXIS: WRITE(W_MS1_PIN, ms1); break
  3272. #endif
  3273. #if HAS_E0_MS_PINS
  3274. case E_AXIS: WRITE(E0_MS1_PIN, ms1); break;
  3275. #endif
  3276. #if HAS_E1_MS_PINS
  3277. case (E_AXIS + 1): WRITE(E1_MS1_PIN, ms1); break;
  3278. #endif
  3279. #if HAS_E2_MS_PINS
  3280. case (E_AXIS + 2): WRITE(E2_MS1_PIN, ms1); break;
  3281. #endif
  3282. #if HAS_E3_MS_PINS
  3283. case (E_AXIS + 3): WRITE(E3_MS1_PIN, ms1); break;
  3284. #endif
  3285. #if HAS_E4_MS_PINS
  3286. case (E_AXIS + 4): WRITE(E4_MS1_PIN, ms1); break;
  3287. #endif
  3288. #if HAS_E5_MS_PINS
  3289. case (E_AXIS + 5): WRITE(E5_MS1_PIN, ms1); break;
  3290. #endif
  3291. #if HAS_E6_MS_PINS
  3292. case (E_AXIS + 6): WRITE(E6_MS1_PIN, ms1); break;
  3293. #endif
  3294. #if HAS_E7_MS_PINS
  3295. case (E_AXIS + 7): WRITE(E7_MS1_PIN, ms1); break;
  3296. #endif
  3297. }
  3298. if (ms2 >= 0) switch (driver) {
  3299. #if HAS_X_MS_PINS || HAS_X2_MS_PINS
  3300. case X_AXIS:
  3301. #if HAS_X_MS_PINS
  3302. WRITE(X_MS2_PIN, ms2);
  3303. #endif
  3304. #if HAS_X2_MS_PINS
  3305. WRITE(X2_MS2_PIN, ms2);
  3306. #endif
  3307. break;
  3308. #endif
  3309. #if HAS_Y_MS_PINS || HAS_Y2_MS_PINS
  3310. case Y_AXIS:
  3311. #if HAS_Y_MS_PINS
  3312. WRITE(Y_MS2_PIN, ms2);
  3313. #endif
  3314. #if HAS_Y2_MS_PINS
  3315. WRITE(Y2_MS2_PIN, ms2);
  3316. #endif
  3317. break;
  3318. #endif
  3319. #if HAS_SOME_Z_MS_PINS
  3320. case Z_AXIS:
  3321. #if HAS_Z_MS_PINS
  3322. WRITE(Z_MS2_PIN, ms2);
  3323. #endif
  3324. #if HAS_Z2_MS_PINS
  3325. WRITE(Z2_MS2_PIN, ms2);
  3326. #endif
  3327. #if HAS_Z3_MS_PINS
  3328. WRITE(Z3_MS2_PIN, ms2);
  3329. #endif
  3330. #if HAS_Z4_MS_PINS
  3331. WRITE(Z4_MS2_PIN, ms2);
  3332. #endif
  3333. break;
  3334. #endif
  3335. #if HAS_I_MS_PINS
  3336. case I_AXIS: WRITE(I_MS2_PIN, ms2); break
  3337. #endif
  3338. #if HAS_J_MS_PINS
  3339. case J_AXIS: WRITE(J_MS2_PIN, ms2); break
  3340. #endif
  3341. #if HAS_K_MS_PINS
  3342. case K_AXIS: WRITE(K_MS2_PIN, ms2); break
  3343. #endif
  3344. #if HAS_U_MS_PINS
  3345. case U_AXIS: WRITE(U_MS2_PIN, ms2); break
  3346. #endif
  3347. #if HAS_V_MS_PINS
  3348. case V_AXIS: WRITE(V_MS2_PIN, ms2); break
  3349. #endif
  3350. #if HAS_W_MS_PINS
  3351. case W_AXIS: WRITE(W_MS2_PIN, ms2); break
  3352. #endif
  3353. #if HAS_E0_MS_PINS
  3354. case E_AXIS: WRITE(E0_MS2_PIN, ms2); break;
  3355. #endif
  3356. #if HAS_E1_MS_PINS
  3357. case (E_AXIS + 1): WRITE(E1_MS2_PIN, ms2); break;
  3358. #endif
  3359. #if HAS_E2_MS_PINS
  3360. case (E_AXIS + 2): WRITE(E2_MS2_PIN, ms2); break;
  3361. #endif
  3362. #if HAS_E3_MS_PINS
  3363. case (E_AXIS + 3): WRITE(E3_MS2_PIN, ms2); break;
  3364. #endif
  3365. #if HAS_E4_MS_PINS
  3366. case (E_AXIS + 4): WRITE(E4_MS2_PIN, ms2); break;
  3367. #endif
  3368. #if HAS_E5_MS_PINS
  3369. case (E_AXIS + 5): WRITE(E5_MS2_PIN, ms2); break;
  3370. #endif
  3371. #if HAS_E6_MS_PINS
  3372. case (E_AXIS + 6): WRITE(E6_MS2_PIN, ms2); break;
  3373. #endif
  3374. #if HAS_E7_MS_PINS
  3375. case (E_AXIS + 7): WRITE(E7_MS2_PIN, ms2); break;
  3376. #endif
  3377. }
  3378. if (ms3 >= 0) switch (driver) {
  3379. #if HAS_X_MS_PINS || HAS_X2_MS_PINS
  3380. case X_AXIS:
  3381. #if HAS_X_MS_PINS && PIN_EXISTS(X_MS3)
  3382. WRITE(X_MS3_PIN, ms3);
  3383. #endif
  3384. #if HAS_X2_MS_PINS && PIN_EXISTS(X2_MS3)
  3385. WRITE(X2_MS3_PIN, ms3);
  3386. #endif
  3387. break;
  3388. #endif
  3389. #if HAS_Y_MS_PINS || HAS_Y2_MS_PINS
  3390. case Y_AXIS:
  3391. #if HAS_Y_MS_PINS && PIN_EXISTS(Y_MS3)
  3392. WRITE(Y_MS3_PIN, ms3);
  3393. #endif
  3394. #if HAS_Y2_MS_PINS && PIN_EXISTS(Y2_MS3)
  3395. WRITE(Y2_MS3_PIN, ms3);
  3396. #endif
  3397. break;
  3398. #endif
  3399. #if HAS_SOME_Z_MS_PINS
  3400. case Z_AXIS:
  3401. #if HAS_Z_MS_PINS && PIN_EXISTS(Z_MS3)
  3402. WRITE(Z_MS3_PIN, ms3);
  3403. #endif
  3404. #if HAS_Z2_MS_PINS && PIN_EXISTS(Z2_MS3)
  3405. WRITE(Z2_MS3_PIN, ms3);
  3406. #endif
  3407. #if HAS_Z3_MS_PINS && PIN_EXISTS(Z3_MS3)
  3408. WRITE(Z3_MS3_PIN, ms3);
  3409. #endif
  3410. #if HAS_Z4_MS_PINS && PIN_EXISTS(Z4_MS3)
  3411. WRITE(Z4_MS3_PIN, ms3);
  3412. #endif
  3413. break;
  3414. #endif
  3415. #if HAS_I_MS_PINS
  3416. case I_AXIS: WRITE(I_MS3_PIN, ms3); break
  3417. #endif
  3418. #if HAS_J_MS_PINS
  3419. case J_AXIS: WRITE(J_MS3_PIN, ms3); break
  3420. #endif
  3421. #if HAS_K_MS_PINS
  3422. case K_AXIS: WRITE(K_MS3_PIN, ms3); break
  3423. #endif
  3424. #if HAS_U_MS_PINS
  3425. case U_AXIS: WRITE(U_MS3_PIN, ms3); break
  3426. #endif
  3427. #if HAS_V_MS_PINS
  3428. case V_AXIS: WRITE(V_MS3_PIN, ms3); break
  3429. #endif
  3430. #if HAS_W_MS_PINS
  3431. case W_AXIS: WRITE(W_MS3_PIN, ms3); break
  3432. #endif
  3433. #if HAS_E0_MS_PINS && PIN_EXISTS(E0_MS3)
  3434. case E_AXIS: WRITE(E0_MS3_PIN, ms3); break;
  3435. #endif
  3436. #if HAS_E1_MS_PINS && PIN_EXISTS(E1_MS3)
  3437. case (E_AXIS + 1): WRITE(E1_MS3_PIN, ms3); break;
  3438. #endif
  3439. #if HAS_E2_MS_PINS && PIN_EXISTS(E2_MS3)
  3440. case (E_AXIS + 2): WRITE(E2_MS3_PIN, ms3); break;
  3441. #endif
  3442. #if HAS_E3_MS_PINS && PIN_EXISTS(E3_MS3)
  3443. case (E_AXIS + 3): WRITE(E3_MS3_PIN, ms3); break;
  3444. #endif
  3445. #if HAS_E4_MS_PINS && PIN_EXISTS(E4_MS3)
  3446. case (E_AXIS + 4): WRITE(E4_MS3_PIN, ms3); break;
  3447. #endif
  3448. #if HAS_E5_MS_PINS && PIN_EXISTS(E5_MS3)
  3449. case (E_AXIS + 5): WRITE(E5_MS3_PIN, ms3); break;
  3450. #endif
  3451. #if HAS_E6_MS_PINS && PIN_EXISTS(E6_MS3)
  3452. case (E_AXIS + 6): WRITE(E6_MS3_PIN, ms3); break;
  3453. #endif
  3454. #if HAS_E7_MS_PINS && PIN_EXISTS(E7_MS3)
  3455. case (E_AXIS + 7): WRITE(E7_MS3_PIN, ms3); break;
  3456. #endif
  3457. }
  3458. }
  3459. void Stepper::microstep_mode(const uint8_t driver, const uint8_t stepping_mode) {
  3460. switch (stepping_mode) {
  3461. #if HAS_MICROSTEP1
  3462. case 1: microstep_ms(driver, MICROSTEP1); break;
  3463. #endif
  3464. #if HAS_MICROSTEP2
  3465. case 2: microstep_ms(driver, MICROSTEP2); break;
  3466. #endif
  3467. #if HAS_MICROSTEP4
  3468. case 4: microstep_ms(driver, MICROSTEP4); break;
  3469. #endif
  3470. #if HAS_MICROSTEP8
  3471. case 8: microstep_ms(driver, MICROSTEP8); break;
  3472. #endif
  3473. #if HAS_MICROSTEP16
  3474. case 16: microstep_ms(driver, MICROSTEP16); break;
  3475. #endif
  3476. #if HAS_MICROSTEP32
  3477. case 32: microstep_ms(driver, MICROSTEP32); break;
  3478. #endif
  3479. #if HAS_MICROSTEP64
  3480. case 64: microstep_ms(driver, MICROSTEP64); break;
  3481. #endif
  3482. #if HAS_MICROSTEP128
  3483. case 128: microstep_ms(driver, MICROSTEP128); break;
  3484. #endif
  3485. default: SERIAL_ERROR_MSG("Microsteps unavailable"); break;
  3486. }
  3487. }
  3488. void Stepper::microstep_readings() {
  3489. #define PIN_CHAR(P) SERIAL_CHAR('0' + READ(P##_PIN))
  3490. #define MS_LINE(A) do{ SERIAL_ECHOPGM(" " STRINGIFY(A) ":"); PIN_CHAR(A##_MS1); PIN_CHAR(A##_MS2); }while(0)
  3491. SERIAL_ECHOPGM("MS1|2|3 Pins");
  3492. #if HAS_X_MS_PINS
  3493. MS_LINE(X);
  3494. #if PIN_EXISTS(X_MS3)
  3495. PIN_CHAR(X_MS3);
  3496. #endif
  3497. #endif
  3498. #if HAS_Y_MS_PINS
  3499. MS_LINE(Y);
  3500. #if PIN_EXISTS(Y_MS3)
  3501. PIN_CHAR(Y_MS3);
  3502. #endif
  3503. #endif
  3504. #if HAS_Z_MS_PINS
  3505. MS_LINE(Z);
  3506. #if PIN_EXISTS(Z_MS3)
  3507. PIN_CHAR(Z_MS3);
  3508. #endif
  3509. #endif
  3510. #if HAS_I_MS_PINS
  3511. MS_LINE(I);
  3512. #if PIN_EXISTS(I_MS3)
  3513. PIN_CHAR(I_MS3);
  3514. #endif
  3515. #endif
  3516. #if HAS_J_MS_PINS
  3517. MS_LINE(J);
  3518. #if PIN_EXISTS(J_MS3)
  3519. PIN_CHAR(J_MS3);
  3520. #endif
  3521. #endif
  3522. #if HAS_K_MS_PINS
  3523. MS_LINE(K);
  3524. #if PIN_EXISTS(K_MS3)
  3525. PIN_CHAR(K_MS3);
  3526. #endif
  3527. #endif
  3528. #if HAS_U_MS_PINS
  3529. MS_LINE(U);
  3530. #if PIN_EXISTS(U_MS3)
  3531. PIN_CHAR(U_MS3);
  3532. #endif
  3533. #endif
  3534. #if HAS_V_MS_PINS
  3535. MS_LINE(V);
  3536. #if PIN_EXISTS(V_MS3)
  3537. PIN_CHAR(V_MS3);
  3538. #endif
  3539. #endif
  3540. #if HAS_W_MS_PINS
  3541. MS_LINE(W);
  3542. #if PIN_EXISTS(W_MS3)
  3543. PIN_CHAR(W_MS3);
  3544. #endif
  3545. #endif
  3546. #if HAS_E0_MS_PINS
  3547. MS_LINE(E0);
  3548. #if PIN_EXISTS(E0_MS3)
  3549. PIN_CHAR(E0_MS3);
  3550. #endif
  3551. #endif
  3552. #if HAS_E1_MS_PINS
  3553. MS_LINE(E1);
  3554. #if PIN_EXISTS(E1_MS3)
  3555. PIN_CHAR(E1_MS3);
  3556. #endif
  3557. #endif
  3558. #if HAS_E2_MS_PINS
  3559. MS_LINE(E2);
  3560. #if PIN_EXISTS(E2_MS3)
  3561. PIN_CHAR(E2_MS3);
  3562. #endif
  3563. #endif
  3564. #if HAS_E3_MS_PINS
  3565. MS_LINE(E3);
  3566. #if PIN_EXISTS(E3_MS3)
  3567. PIN_CHAR(E3_MS3);
  3568. #endif
  3569. #endif
  3570. #if HAS_E4_MS_PINS
  3571. MS_LINE(E4);
  3572. #if PIN_EXISTS(E4_MS3)
  3573. PIN_CHAR(E4_MS3);
  3574. #endif
  3575. #endif
  3576. #if HAS_E5_MS_PINS
  3577. MS_LINE(E5);
  3578. #if PIN_EXISTS(E5_MS3)
  3579. PIN_CHAR(E5_MS3);
  3580. #endif
  3581. #endif
  3582. #if HAS_E6_MS_PINS
  3583. MS_LINE(E6);
  3584. #if PIN_EXISTS(E6_MS3)
  3585. PIN_CHAR(E6_MS3);
  3586. #endif
  3587. #endif
  3588. #if HAS_E7_MS_PINS
  3589. MS_LINE(E7);
  3590. #if PIN_EXISTS(E7_MS3)
  3591. PIN_CHAR(E7_MS3);
  3592. #endif
  3593. #endif
  3594. SERIAL_EOL();
  3595. }
  3596. #endif // HAS_MICROSTEPS