My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stepper.cpp 126KB

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