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

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