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.

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