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

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