My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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