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

stepper.cpp 127KB

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