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 131KB

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