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

stepper.cpp 108KB

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