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

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