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

stepper.cpp 96KB

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