My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

stepper.cpp 104KB

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