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

stepper.cpp 105KB

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