stepper.cpp 93 KB

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