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

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