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

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