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

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