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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  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. long 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. long 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 long 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. }
  1062. void Stepper::isr() {
  1063. #define ENDSTOP_NOMINAL_OCR_VAL 1500 * HAL_TICKS_PER_US // Check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch
  1064. #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
  1065. #if DISABLED(LIN_ADVANCE)
  1066. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  1067. DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
  1068. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1069. #ifndef CPU_32_BIT
  1070. sei();
  1071. #endif
  1072. #endif
  1073. hal_timer_t ocr_val;
  1074. static uint32_t step_remaining = 0; // SPLIT function always runs. This allows 16 bit timers to be
  1075. // used to generate the stepper ISR.
  1076. #define SPLIT(L) do { \
  1077. if (L > ENDSTOP_NOMINAL_OCR_VAL) { \
  1078. const uint32_t remainder = (uint32_t)L % (ENDSTOP_NOMINAL_OCR_VAL); \
  1079. ocr_val = (remainder < OCR_VAL_TOLERANCE) ? ENDSTOP_NOMINAL_OCR_VAL + remainder : ENDSTOP_NOMINAL_OCR_VAL; \
  1080. step_remaining = (uint32_t)L - ocr_val; \
  1081. } \
  1082. else \
  1083. ocr_val = L;\
  1084. }while(0)
  1085. // Time remaining before the next step?
  1086. if (step_remaining) {
  1087. // Make sure endstops are updated
  1088. if (ENDSTOPS_ENABLED) endstops.update();
  1089. // Next ISR either for endstops or stepping
  1090. ocr_val = step_remaining <= ENDSTOP_NOMINAL_OCR_VAL ? step_remaining : ENDSTOP_NOMINAL_OCR_VAL;
  1091. step_remaining -= ocr_val;
  1092. _NEXT_ISR(ocr_val);
  1093. #if DISABLED(LIN_ADVANCE)
  1094. HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
  1095. HAL_ENABLE_ISRs();
  1096. #endif
  1097. return;
  1098. }
  1099. //
  1100. // When cleaning, discard the current block and run fast
  1101. //
  1102. if (cleaning_buffer_counter) {
  1103. if (cleaning_buffer_counter < 0) { // Count up for endstop hit
  1104. if (current_block) planner.discard_current_block(); // Discard the active block that led to the trigger
  1105. if (!planner.discard_continued_block()) // Discard next CONTINUED block
  1106. cleaning_buffer_counter = 0; // Keep discarding until non-CONTINUED
  1107. }
  1108. else {
  1109. planner.discard_current_block();
  1110. --cleaning_buffer_counter; // Count down for abort print
  1111. #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
  1112. if (!cleaning_buffer_counter) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  1113. #endif
  1114. }
  1115. current_block = NULL; // Prep to get a new block after cleaning
  1116. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 10000); // Run at max speed - 10 KHz
  1117. HAL_ENABLE_ISRs();
  1118. return;
  1119. }
  1120. // If there is no current block, attempt to pop one from the buffer
  1121. if (!current_block) {
  1122. // Anything in the buffer?
  1123. if ((current_block = planner.get_current_block())) {
  1124. // Initialize the trapezoid generator from the current block.
  1125. static int8_t last_extruder = -1;
  1126. #if ENABLED(LIN_ADVANCE)
  1127. #if E_STEPPERS > 1
  1128. if (current_block->active_extruder != last_extruder) {
  1129. current_adv_steps = 0; // If the now active extruder wasn't in use during the last move, its pressure is most likely gone.
  1130. LA_active_extruder = current_block->active_extruder;
  1131. }
  1132. #endif
  1133. if ((use_advance_lead = current_block->use_advance_lead)) {
  1134. LA_decelerate_after = current_block->decelerate_after;
  1135. final_adv_steps = current_block->final_adv_steps;
  1136. max_adv_steps = current_block->max_adv_steps;
  1137. }
  1138. #endif
  1139. if (current_block->direction_bits != last_direction_bits || current_block->active_extruder != last_extruder) {
  1140. last_direction_bits = current_block->direction_bits;
  1141. last_extruder = current_block->active_extruder;
  1142. set_directions();
  1143. }
  1144. // No acceleration / deceleration time elapsed so far
  1145. acceleration_time = deceleration_time = 0;
  1146. // No step events completed so far
  1147. step_events_completed = 0;
  1148. // step_rate to timer interval
  1149. OCR1A_nominal = calc_timer_interval(current_block->nominal_rate);
  1150. // make a note of the number of step loops required at nominal speed
  1151. step_loops_nominal = step_loops;
  1152. #if DISABLED(BEZIER_JERK_CONTROL)
  1153. // Set as deceleration point the initial rate of the block
  1154. acc_step_rate = current_block->initial_rate;
  1155. #endif
  1156. #if ENABLED(BEZIER_JERK_CONTROL)
  1157. // Initialize the Bézier speed curve
  1158. _calc_bezier_curve_coeffs(current_block->initial_rate, current_block->cruise_rate, current_block->acceleration_time_inverse);
  1159. // We have not started the 2nd half of the trapezoid
  1160. bezier_2nd_half = false;
  1161. #endif
  1162. // Initialize Bresenham counters to 1/2 the ceiling
  1163. counter_X = counter_Y = counter_Z = counter_E = -(current_block->step_event_count >> 1);
  1164. #if ENABLED(MIXING_EXTRUDER)
  1165. MIXING_STEPPERS_LOOP(i)
  1166. counter_m[i] = -(current_block->mix_event_count[i] >> 1);
  1167. #endif
  1168. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1169. e_hit = 2; // Needed for the case an endstop is already triggered before the new move begins.
  1170. // No 'change' can be detected.
  1171. #endif
  1172. #if ENABLED(Z_LATE_ENABLE)
  1173. // If delayed Z enable, postpone move for 1mS
  1174. if (current_block->steps[Z_AXIS] > 0) {
  1175. enable_Z();
  1176. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
  1177. HAL_ENABLE_ISRs();
  1178. return;
  1179. }
  1180. #endif
  1181. }
  1182. else {
  1183. // If no more queued moves, postpone next check for 1mS
  1184. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
  1185. HAL_ENABLE_ISRs();
  1186. return;
  1187. }
  1188. }
  1189. // Update endstops state, if enabled
  1190. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1191. if (e_hit && ENDSTOPS_ENABLED) {
  1192. endstops.update();
  1193. e_hit--;
  1194. }
  1195. #else
  1196. if (ENDSTOPS_ENABLED) endstops.update();
  1197. #endif
  1198. // Take multiple steps per interrupt (For high speed moves)
  1199. bool all_steps_done = false;
  1200. for (uint8_t i = step_loops; i--;) {
  1201. #define _COUNTER(AXIS) counter_## AXIS
  1202. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  1203. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  1204. // Advance the Bresenham counter; start a pulse if the axis needs a step
  1205. #define PULSE_START(AXIS) do{ \
  1206. _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
  1207. if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); } \
  1208. }while(0)
  1209. // Advance the Bresenham counter; start a pulse if the axis needs a step
  1210. #define STEP_TICK(AXIS) do { \
  1211. if (_COUNTER(AXIS) > 0) { \
  1212. _COUNTER(AXIS) -= current_block->step_event_count; \
  1213. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  1214. } \
  1215. }while(0)
  1216. // Stop an active pulse, if any
  1217. #define PULSE_STOP(AXIS) _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), 0)
  1218. /**
  1219. * Estimate the number of cycles that the stepper logic already takes
  1220. * up between the start and stop of the X stepper pulse.
  1221. *
  1222. * Currently this uses very modest estimates of around 5 cycles.
  1223. * True values may be derived by careful testing.
  1224. *
  1225. * Once any delay is added, the cost of the delay code itself
  1226. * may be subtracted from this value to get a more accurate delay.
  1227. * Delays under 20 cycles (1.25µs) will be very accurate, using NOPs.
  1228. * Longer delays use a loop. The resolution is 8 cycles.
  1229. */
  1230. #if HAS_X_STEP
  1231. #define _CYCLE_APPROX_1 5
  1232. #else
  1233. #define _CYCLE_APPROX_1 0
  1234. #endif
  1235. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  1236. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1 + 4
  1237. #else
  1238. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1
  1239. #endif
  1240. #if HAS_Y_STEP
  1241. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2 + 5
  1242. #else
  1243. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2
  1244. #endif
  1245. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  1246. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3 + 4
  1247. #else
  1248. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3
  1249. #endif
  1250. #if HAS_Z_STEP
  1251. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4 + 5
  1252. #else
  1253. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4
  1254. #endif
  1255. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  1256. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5 + 4
  1257. #else
  1258. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5
  1259. #endif
  1260. #if DISABLED(LIN_ADVANCE)
  1261. #if ENABLED(MIXING_EXTRUDER)
  1262. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + (MIXING_STEPPERS) * 6
  1263. #else
  1264. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + 5
  1265. #endif
  1266. #else
  1267. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6
  1268. #endif
  1269. #define CYCLES_EATEN_XYZE _CYCLE_APPROX_7
  1270. #define EXTRA_CYCLES_XYZE (STEP_PULSE_CYCLES - (CYCLES_EATEN_XYZE))
  1271. /**
  1272. * If a minimum pulse time was specified get the timer 0 value.
  1273. *
  1274. * On AVR the TCNT0 timer has an 8x prescaler, so it increments every 8 cycles.
  1275. * That's every 0.5µs on 16MHz and every 0.4µs on 20MHz.
  1276. * 20 counts of TCNT0 -by itself- is a good pulse delay.
  1277. * 10µs = 160 or 200 cycles.
  1278. */
  1279. #if EXTRA_CYCLES_XYZE > 20
  1280. hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  1281. #endif
  1282. #if HAS_X_STEP
  1283. PULSE_START(X);
  1284. #endif
  1285. #if HAS_Y_STEP
  1286. PULSE_START(Y);
  1287. #endif
  1288. #if HAS_Z_STEP
  1289. PULSE_START(Z);
  1290. #endif
  1291. #if ENABLED(LIN_ADVANCE)
  1292. counter_E += current_block->steps[E_AXIS];
  1293. if (counter_E > 0) {
  1294. #if DISABLED(MIXING_EXTRUDER)
  1295. // Don't step E here for mixing extruder
  1296. motor_direction(E_AXIS) ? --e_steps : ++e_steps;
  1297. #endif
  1298. }
  1299. #if ENABLED(MIXING_EXTRUDER)
  1300. // Step mixing steppers proportionally
  1301. const bool dir = motor_direction(E_AXIS);
  1302. MIXING_STEPPERS_LOOP(j) {
  1303. counter_m[j] += current_block->steps[E_AXIS];
  1304. if (counter_m[j] > 0) {
  1305. counter_m[j] -= current_block->mix_event_count[j];
  1306. dir ? --e_steps[j] : ++e_steps[j];
  1307. }
  1308. }
  1309. #endif
  1310. #else // !LIN_ADVANCE - use linear interpolation for E also
  1311. #if ENABLED(MIXING_EXTRUDER)
  1312. // Keep updating the single E axis
  1313. counter_E += current_block->steps[E_AXIS];
  1314. // Tick the counters used for this mix
  1315. MIXING_STEPPERS_LOOP(j) {
  1316. // Step mixing steppers (proportionally)
  1317. counter_m[j] += current_block->steps[E_AXIS];
  1318. // Step when the counter goes over zero
  1319. if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
  1320. }
  1321. #else // !MIXING_EXTRUDER
  1322. PULSE_START(E);
  1323. #endif
  1324. #endif // !LIN_ADVANCE
  1325. #if HAS_X_STEP
  1326. STEP_TICK(X);
  1327. #endif
  1328. #if HAS_Y_STEP
  1329. STEP_TICK(Y);
  1330. #endif
  1331. #if HAS_Z_STEP
  1332. STEP_TICK(Z);
  1333. #endif
  1334. STEP_TICK(E); // Always tick the single E axis
  1335. // For minimum pulse time wait before stopping pulses
  1336. #if EXTRA_CYCLES_XYZE > 20
  1337. while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1338. pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  1339. #elif EXTRA_CYCLES_XYZE > 0
  1340. DELAY_NOPS(EXTRA_CYCLES_XYZE);
  1341. #endif
  1342. #if HAS_X_STEP
  1343. PULSE_STOP(X);
  1344. #endif
  1345. #if HAS_Y_STEP
  1346. PULSE_STOP(Y);
  1347. #endif
  1348. #if HAS_Z_STEP
  1349. PULSE_STOP(Z);
  1350. #endif
  1351. #if DISABLED(LIN_ADVANCE)
  1352. #if ENABLED(MIXING_EXTRUDER)
  1353. MIXING_STEPPERS_LOOP(j) {
  1354. if (counter_m[j] > 0) {
  1355. counter_m[j] -= current_block->mix_event_count[j];
  1356. En_STEP_WRITE(j, INVERT_E_STEP_PIN);
  1357. }
  1358. }
  1359. #else // !MIXING_EXTRUDER
  1360. PULSE_STOP(E);
  1361. #endif
  1362. #endif // !LIN_ADVANCE
  1363. if (++step_events_completed >= current_block->step_event_count) {
  1364. all_steps_done = true;
  1365. break;
  1366. }
  1367. // For minimum pulse time wait after stopping pulses also
  1368. #if EXTRA_CYCLES_XYZE > 20
  1369. if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1370. #elif EXTRA_CYCLES_XYZE > 0
  1371. if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE);
  1372. #endif
  1373. } // steps_loop
  1374. // Calculate new timer value
  1375. if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
  1376. #if ENABLED(BEZIER_JERK_CONTROL)
  1377. // Get the next speed to use (Jerk limited!)
  1378. hal_timer_t acc_step_rate =
  1379. acceleration_time < current_block->acceleration_time
  1380. ? _eval_bezier_curve(acceleration_time)
  1381. : current_block->cruise_rate;
  1382. #else
  1383. #ifdef CPU_32_BIT
  1384. MultiU32X24toH32(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  1385. #else
  1386. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  1387. #endif
  1388. acc_step_rate += current_block->initial_rate;
  1389. // upper limit
  1390. NOMORE(acc_step_rate, current_block->nominal_rate);
  1391. #endif
  1392. // step_rate to timer interval
  1393. const hal_timer_t interval = calc_timer_interval(acc_step_rate);
  1394. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  1395. _NEXT_ISR(ocr_val);
  1396. acceleration_time += interval;
  1397. #if ENABLED(LIN_ADVANCE)
  1398. if (current_block->use_advance_lead) {
  1399. if (step_events_completed == step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  1400. nextAdvanceISR = 0; // Wake up eISR on first acceleration loop and fire ISR if final adv_rate is reached
  1401. eISR_Rate = current_block->advance_speed;
  1402. }
  1403. }
  1404. else {
  1405. eISR_Rate = ADV_NEVER;
  1406. if (e_steps) nextAdvanceISR = 0;
  1407. }
  1408. #endif // LIN_ADVANCE
  1409. }
  1410. else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
  1411. hal_timer_t step_rate;
  1412. #if ENABLED(BEZIER_JERK_CONTROL)
  1413. // If this is the 1st time we process the 2nd half of the trapezoid...
  1414. if (!bezier_2nd_half) {
  1415. // Initialize the Bézier speed curve
  1416. _calc_bezier_curve_coeffs(current_block->cruise_rate, current_block->final_rate, current_block->deceleration_time_inverse);
  1417. bezier_2nd_half = true;
  1418. }
  1419. // Calculate the next speed to use
  1420. step_rate = deceleration_time < current_block->deceleration_time
  1421. ? _eval_bezier_curve(deceleration_time)
  1422. : current_block->final_rate;
  1423. #else
  1424. // Using the old trapezoidal control
  1425. #ifdef CPU_32_BIT
  1426. MultiU32X24toH32(step_rate, deceleration_time, current_block->acceleration_rate);
  1427. #else
  1428. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  1429. #endif
  1430. if (step_rate < acc_step_rate) { // Still decelerating?
  1431. step_rate = acc_step_rate - step_rate;
  1432. NOLESS(step_rate, current_block->final_rate);
  1433. }
  1434. else
  1435. step_rate = current_block->final_rate;
  1436. #endif
  1437. // step_rate to timer interval
  1438. const hal_timer_t interval = calc_timer_interval(step_rate);
  1439. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  1440. _NEXT_ISR(ocr_val);
  1441. deceleration_time += interval;
  1442. #if ENABLED(LIN_ADVANCE)
  1443. if (current_block->use_advance_lead) {
  1444. if (step_events_completed <= (uint32_t)current_block->decelerate_after + step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  1445. nextAdvanceISR = 0; // Wake up eISR on first deceleration loop
  1446. eISR_Rate = current_block->advance_speed;
  1447. }
  1448. }
  1449. else {
  1450. eISR_Rate = ADV_NEVER;
  1451. if (e_steps) nextAdvanceISR = 0;
  1452. }
  1453. #endif // LIN_ADVANCE
  1454. }
  1455. else {
  1456. #if ENABLED(LIN_ADVANCE)
  1457. // If we have esteps to execute, fire the next advance_isr "now"
  1458. if (e_steps && eISR_Rate != current_block->advance_speed) nextAdvanceISR = 0;
  1459. #endif
  1460. SPLIT(OCR1A_nominal); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  1461. _NEXT_ISR(ocr_val);
  1462. // ensure we're running at the correct step rate, even if we just came off an acceleration
  1463. step_loops = step_loops_nominal;
  1464. }
  1465. #if DISABLED(LIN_ADVANCE)
  1466. // Make sure stepper ISR doesn't monopolize the CPU
  1467. HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
  1468. #endif
  1469. // If current block is finished, reset pointer
  1470. if (all_steps_done) {
  1471. current_block = NULL;
  1472. planner.discard_current_block();
  1473. }
  1474. #if DISABLED(LIN_ADVANCE)
  1475. HAL_ENABLE_ISRs();
  1476. #endif
  1477. }
  1478. #if ENABLED(LIN_ADVANCE)
  1479. #define CYCLES_EATEN_E (E_STEPPERS * 5)
  1480. #define EXTRA_CYCLES_E (STEP_PULSE_CYCLES - (CYCLES_EATEN_E))
  1481. // Timer interrupt for E. e_steps is set in the main routine;
  1482. void Stepper::advance_isr() {
  1483. #if ENABLED(MK2_MULTIPLEXER) // For SNMM even-numbered steppers are reversed
  1484. #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)
  1485. #elif ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  1486. #define SET_E_STEP_DIR(INDEX) do{ if (e_steps) { if (e_steps < 0) REV_E_DIR(); else NORM_E_DIR(); } }while(0)
  1487. #else
  1488. #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)
  1489. #endif
  1490. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  1491. #define START_E_PULSE(INDEX) do{ if (e_steps) E_STEP_WRITE(!INVERT_E_STEP_PIN); }while(0)
  1492. #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)
  1493. #else
  1494. #define START_E_PULSE(INDEX) do{ if (e_steps) E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); }while(0)
  1495. #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)
  1496. #endif
  1497. if (use_advance_lead) {
  1498. if (step_events_completed > LA_decelerate_after && current_adv_steps > final_adv_steps) {
  1499. e_steps--;
  1500. current_adv_steps--;
  1501. nextAdvanceISR = eISR_Rate;
  1502. }
  1503. else if (step_events_completed < LA_decelerate_after && current_adv_steps < max_adv_steps) {
  1504. //step_events_completed <= (uint32_t)current_block->accelerate_until) {
  1505. e_steps++;
  1506. current_adv_steps++;
  1507. nextAdvanceISR = eISR_Rate;
  1508. }
  1509. else {
  1510. nextAdvanceISR = ADV_NEVER;
  1511. eISR_Rate = ADV_NEVER;
  1512. }
  1513. }
  1514. else
  1515. nextAdvanceISR = ADV_NEVER;
  1516. switch (LA_active_extruder) {
  1517. case 0: SET_E_STEP_DIR(0); break;
  1518. #if EXTRUDERS > 1
  1519. case 1: SET_E_STEP_DIR(1); break;
  1520. #if EXTRUDERS > 2
  1521. case 2: SET_E_STEP_DIR(2); break;
  1522. #if EXTRUDERS > 3
  1523. case 3: SET_E_STEP_DIR(3); break;
  1524. #if EXTRUDERS > 4
  1525. case 4: SET_E_STEP_DIR(4); break;
  1526. #endif // EXTRUDERS > 4
  1527. #endif // EXTRUDERS > 3
  1528. #endif // EXTRUDERS > 2
  1529. #endif // EXTRUDERS > 1
  1530. }
  1531. // Step E stepper if we have steps
  1532. while (e_steps) {
  1533. #if EXTRA_CYCLES_E > 20
  1534. hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  1535. #endif
  1536. switch (LA_active_extruder) {
  1537. case 0: START_E_PULSE(0); break;
  1538. #if EXTRUDERS > 1
  1539. case 1: START_E_PULSE(1); break;
  1540. #if EXTRUDERS > 2
  1541. case 2: START_E_PULSE(2); break;
  1542. #if EXTRUDERS > 3
  1543. case 3: START_E_PULSE(3); break;
  1544. #if EXTRUDERS > 4
  1545. case 4: START_E_PULSE(4); break;
  1546. #endif // EXTRUDERS > 4
  1547. #endif // EXTRUDERS > 3
  1548. #endif // EXTRUDERS > 2
  1549. #endif // EXTRUDERS > 1
  1550. }
  1551. // For minimum pulse time wait before stopping pulses
  1552. #if EXTRA_CYCLES_E > 20
  1553. while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1554. pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  1555. #elif EXTRA_CYCLES_E > 0
  1556. DELAY_NOPS(EXTRA_CYCLES_E);
  1557. #endif
  1558. switch (LA_active_extruder) {
  1559. case 0: STOP_E_PULSE(0); break;
  1560. #if EXTRUDERS > 1
  1561. case 1: STOP_E_PULSE(1); break;
  1562. #if EXTRUDERS > 2
  1563. case 2: STOP_E_PULSE(2); break;
  1564. #if EXTRUDERS > 3
  1565. case 3: STOP_E_PULSE(3); break;
  1566. #if EXTRUDERS > 4
  1567. case 4: STOP_E_PULSE(4); break;
  1568. #endif // EXTRUDERS > 4
  1569. #endif // EXTRUDERS > 3
  1570. #endif // EXTRUDERS > 2
  1571. #endif // EXTRUDERS > 1
  1572. }
  1573. // For minimum pulse time wait before looping
  1574. #if EXTRA_CYCLES_E > 20
  1575. if (e_steps) while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1576. #elif EXTRA_CYCLES_E > 0
  1577. if (e_steps) DELAY_NOPS(EXTRA_CYCLES_E);
  1578. #endif
  1579. } // e_steps
  1580. }
  1581. void Stepper::advance_isr_scheduler() {
  1582. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  1583. DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
  1584. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1585. sei();
  1586. // Run main stepping ISR if flagged
  1587. if (!nextMainISR) isr();
  1588. // Run Advance stepping ISR if flagged
  1589. if (!nextAdvanceISR) advance_isr();
  1590. // Is the next advance ISR scheduled before the next main ISR?
  1591. if (nextAdvanceISR <= nextMainISR) {
  1592. // Set up the next interrupt
  1593. HAL_timer_set_compare(STEP_TIMER_NUM, nextAdvanceISR);
  1594. // New interval for the next main ISR
  1595. if (nextMainISR) nextMainISR -= nextAdvanceISR;
  1596. // Will call Stepper::advance_isr on the next interrupt
  1597. nextAdvanceISR = 0;
  1598. }
  1599. else {
  1600. // The next main ISR comes first
  1601. HAL_timer_set_compare(STEP_TIMER_NUM, nextMainISR);
  1602. // New interval for the next advance ISR, if any
  1603. if (nextAdvanceISR && nextAdvanceISR != ADV_NEVER)
  1604. nextAdvanceISR -= nextMainISR;
  1605. // Will call Stepper::isr on the next interrupt
  1606. nextMainISR = 0;
  1607. }
  1608. // Make sure stepper ISR doesn't monopolize the CPU
  1609. HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
  1610. // Restore original ISR settings
  1611. HAL_ENABLE_ISRs();
  1612. }
  1613. #endif // LIN_ADVANCE
  1614. void Stepper::init() {
  1615. // Init Digipot Motor Current
  1616. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  1617. digipot_init();
  1618. #endif
  1619. #if MB(ALLIGATOR)
  1620. const float motor_current[] = MOTOR_CURRENT;
  1621. unsigned int digipot_motor = 0;
  1622. for (uint8_t i = 0; i < 3 + EXTRUDERS; i++) {
  1623. digipot_motor = 255 * (motor_current[i] / 2.5);
  1624. dac084s085::setValue(i, digipot_motor);
  1625. }
  1626. #endif//MB(ALLIGATOR)
  1627. // Init Microstepping Pins
  1628. #if HAS_MICROSTEPS
  1629. microstep_init();
  1630. #endif
  1631. // Init Dir Pins
  1632. #if HAS_X_DIR
  1633. X_DIR_INIT;
  1634. #endif
  1635. #if HAS_X2_DIR
  1636. X2_DIR_INIT;
  1637. #endif
  1638. #if HAS_Y_DIR
  1639. Y_DIR_INIT;
  1640. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  1641. Y2_DIR_INIT;
  1642. #endif
  1643. #endif
  1644. #if HAS_Z_DIR
  1645. Z_DIR_INIT;
  1646. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  1647. Z2_DIR_INIT;
  1648. #endif
  1649. #endif
  1650. #if HAS_E0_DIR
  1651. E0_DIR_INIT;
  1652. #endif
  1653. #if HAS_E1_DIR
  1654. E1_DIR_INIT;
  1655. #endif
  1656. #if HAS_E2_DIR
  1657. E2_DIR_INIT;
  1658. #endif
  1659. #if HAS_E3_DIR
  1660. E3_DIR_INIT;
  1661. #endif
  1662. #if HAS_E4_DIR
  1663. E4_DIR_INIT;
  1664. #endif
  1665. // Init Enable Pins - steppers default to disabled.
  1666. #if HAS_X_ENABLE
  1667. X_ENABLE_INIT;
  1668. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  1669. #if (ENABLED(DUAL_X_CARRIAGE) || ENABLED(X_DUAL_STEPPER_DRIVERS)) && HAS_X2_ENABLE
  1670. X2_ENABLE_INIT;
  1671. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  1672. #endif
  1673. #endif
  1674. #if HAS_Y_ENABLE
  1675. Y_ENABLE_INIT;
  1676. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  1677. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  1678. Y2_ENABLE_INIT;
  1679. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  1680. #endif
  1681. #endif
  1682. #if HAS_Z_ENABLE
  1683. Z_ENABLE_INIT;
  1684. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  1685. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  1686. Z2_ENABLE_INIT;
  1687. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  1688. #endif
  1689. #endif
  1690. #if HAS_E0_ENABLE
  1691. E0_ENABLE_INIT;
  1692. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  1693. #endif
  1694. #if HAS_E1_ENABLE
  1695. E1_ENABLE_INIT;
  1696. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  1697. #endif
  1698. #if HAS_E2_ENABLE
  1699. E2_ENABLE_INIT;
  1700. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  1701. #endif
  1702. #if HAS_E3_ENABLE
  1703. E3_ENABLE_INIT;
  1704. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  1705. #endif
  1706. #if HAS_E4_ENABLE
  1707. E4_ENABLE_INIT;
  1708. if (!E_ENABLE_ON) E4_ENABLE_WRITE(HIGH);
  1709. #endif
  1710. // Init endstops and pullups
  1711. endstops.init();
  1712. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  1713. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  1714. #define _DISABLE(AXIS) disable_## AXIS()
  1715. #define AXIS_INIT(AXIS, PIN) \
  1716. _STEP_INIT(AXIS); \
  1717. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  1718. _DISABLE(AXIS)
  1719. #define E_AXIS_INIT(NUM) AXIS_INIT(E## NUM, E)
  1720. // Init Step Pins
  1721. #if HAS_X_STEP
  1722. #if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)
  1723. X2_STEP_INIT;
  1724. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  1725. #endif
  1726. AXIS_INIT(X, X);
  1727. #endif
  1728. #if HAS_Y_STEP
  1729. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  1730. Y2_STEP_INIT;
  1731. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  1732. #endif
  1733. AXIS_INIT(Y, Y);
  1734. #endif
  1735. #if HAS_Z_STEP
  1736. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  1737. Z2_STEP_INIT;
  1738. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  1739. #endif
  1740. AXIS_INIT(Z, Z);
  1741. #endif
  1742. #if HAS_E0_STEP
  1743. E_AXIS_INIT(0);
  1744. #endif
  1745. #if HAS_E1_STEP
  1746. E_AXIS_INIT(1);
  1747. #endif
  1748. #if HAS_E2_STEP
  1749. E_AXIS_INIT(2);
  1750. #endif
  1751. #if HAS_E3_STEP
  1752. E_AXIS_INIT(3);
  1753. #endif
  1754. #if HAS_E4_STEP
  1755. E_AXIS_INIT(4);
  1756. #endif
  1757. #ifdef __AVR__
  1758. // waveform generation = 0100 = CTC
  1759. SET_WGM(1, CTC_OCRnA);
  1760. // output mode = 00 (disconnected)
  1761. SET_COMA(1, NORMAL);
  1762. // Set the timer pre-scaler
  1763. // Generally we use a divider of 8, resulting in a 2MHz timer
  1764. // frequency on a 16MHz MCU. If you are going to change this, be
  1765. // sure to regenerate speed_lookuptable.h with
  1766. // create_speed_lookuptable.py
  1767. SET_CS(1, PRESCALER_8); // CS 2 = 1/8 prescaler
  1768. // Init Stepper ISR to 122 Hz for quick starting
  1769. OCR1A = 0x4000;
  1770. TCNT1 = 0;
  1771. #else
  1772. // Init Stepper ISR to 122 Hz for quick starting
  1773. HAL_timer_start(STEP_TIMER_NUM, 122);
  1774. #endif
  1775. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1776. endstops.enable(true); // Start with endstops active. After homing they can be disabled
  1777. sei();
  1778. set_directions(); // Init directions to last_direction_bits = 0
  1779. }
  1780. /**
  1781. * Block until all buffered steps are executed / cleaned
  1782. */
  1783. void Stepper::synchronize() { while (planner.has_blocks_queued() || cleaning_buffer_counter) idle(); }
  1784. /**
  1785. * Set the stepper positions directly in steps
  1786. *
  1787. * The input is based on the typical per-axis XYZ steps.
  1788. * For CORE machines XYZ needs to be translated to ABC.
  1789. *
  1790. * This allows get_axis_position_mm to correctly
  1791. * derive the current XYZ position later on.
  1792. */
  1793. void Stepper::set_position(const long &a, const long &b, const long &c, const long &e) {
  1794. synchronize(); // Bad to set stepper counts in the middle of a move
  1795. CRITICAL_SECTION_START;
  1796. #if CORE_IS_XY
  1797. // corexy positioning
  1798. // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
  1799. count_position[A_AXIS] = a + b;
  1800. count_position[B_AXIS] = CORESIGN(a - b);
  1801. count_position[Z_AXIS] = c;
  1802. #elif CORE_IS_XZ
  1803. // corexz planning
  1804. count_position[A_AXIS] = a + c;
  1805. count_position[Y_AXIS] = b;
  1806. count_position[C_AXIS] = CORESIGN(a - c);
  1807. #elif CORE_IS_YZ
  1808. // coreyz planning
  1809. count_position[X_AXIS] = a;
  1810. count_position[B_AXIS] = b + c;
  1811. count_position[C_AXIS] = CORESIGN(b - c);
  1812. #else
  1813. // default non-h-bot planning
  1814. count_position[X_AXIS] = a;
  1815. count_position[Y_AXIS] = b;
  1816. count_position[Z_AXIS] = c;
  1817. #endif
  1818. count_position[E_AXIS] = e;
  1819. CRITICAL_SECTION_END;
  1820. }
  1821. void Stepper::set_position(const AxisEnum &axis, const long &v) {
  1822. CRITICAL_SECTION_START;
  1823. count_position[axis] = v;
  1824. CRITICAL_SECTION_END;
  1825. }
  1826. void Stepper::set_e_position(const long &e) {
  1827. CRITICAL_SECTION_START;
  1828. count_position[E_AXIS] = e;
  1829. CRITICAL_SECTION_END;
  1830. }
  1831. /**
  1832. * Get a stepper's position in steps.
  1833. */
  1834. long Stepper::position(const AxisEnum axis) {
  1835. CRITICAL_SECTION_START;
  1836. const long count_pos = count_position[axis];
  1837. CRITICAL_SECTION_END;
  1838. return count_pos;
  1839. }
  1840. /**
  1841. * Get an axis position according to stepper position(s)
  1842. * For CORE machines apply translation from ABC to XYZ.
  1843. */
  1844. float Stepper::get_axis_position_mm(const AxisEnum axis) {
  1845. float axis_steps;
  1846. #if IS_CORE
  1847. // Requesting one of the "core" axes?
  1848. if (axis == CORE_AXIS_1 || axis == CORE_AXIS_2) {
  1849. CRITICAL_SECTION_START;
  1850. // ((a1+a2)+(a1-a2))/2 -> (a1+a2+a1-a2)/2 -> (a1+a1)/2 -> a1
  1851. // ((a1+a2)-(a1-a2))/2 -> (a1+a2-a1+a2)/2 -> (a2+a2)/2 -> a2
  1852. axis_steps = 0.5f * (
  1853. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1854. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1855. );
  1856. CRITICAL_SECTION_END;
  1857. }
  1858. else
  1859. axis_steps = position(axis);
  1860. #else
  1861. axis_steps = position(axis);
  1862. #endif
  1863. return axis_steps * planner.steps_to_mm[axis];
  1864. }
  1865. void Stepper::finish_and_disable() {
  1866. synchronize();
  1867. disable_all_steppers();
  1868. }
  1869. void Stepper::quick_stop() {
  1870. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1871. kill_current_block();
  1872. current_block = NULL;
  1873. cleaning_buffer_counter = 5000;
  1874. planner.clear_block_buffer();
  1875. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1876. #if ENABLED(ULTRA_LCD)
  1877. planner.clear_block_buffer_runtime();
  1878. #endif
  1879. }
  1880. void Stepper::endstop_triggered(const AxisEnum axis) {
  1881. #if IS_CORE
  1882. endstops_trigsteps[axis] = 0.5f * (
  1883. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1884. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1885. );
  1886. #else // !COREXY && !COREXZ && !COREYZ
  1887. endstops_trigsteps[axis] = count_position[axis];
  1888. #endif // !COREXY && !COREXZ && !COREYZ
  1889. kill_current_block();
  1890. cleaning_buffer_counter = -1; // Discard the rest of the move
  1891. }
  1892. void Stepper::report_positions() {
  1893. CRITICAL_SECTION_START;
  1894. const long xpos = count_position[X_AXIS],
  1895. ypos = count_position[Y_AXIS],
  1896. zpos = count_position[Z_AXIS];
  1897. CRITICAL_SECTION_END;
  1898. #if CORE_IS_XY || CORE_IS_XZ || IS_DELTA || IS_SCARA
  1899. SERIAL_PROTOCOLPGM(MSG_COUNT_A);
  1900. #else
  1901. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1902. #endif
  1903. SERIAL_PROTOCOL(xpos);
  1904. #if CORE_IS_XY || CORE_IS_YZ || IS_DELTA || IS_SCARA
  1905. SERIAL_PROTOCOLPGM(" B:");
  1906. #else
  1907. SERIAL_PROTOCOLPGM(" Y:");
  1908. #endif
  1909. SERIAL_PROTOCOL(ypos);
  1910. #if CORE_IS_XZ || CORE_IS_YZ || IS_DELTA
  1911. SERIAL_PROTOCOLPGM(" C:");
  1912. #else
  1913. SERIAL_PROTOCOLPGM(" Z:");
  1914. #endif
  1915. SERIAL_PROTOCOL(zpos);
  1916. SERIAL_EOL();
  1917. }
  1918. #if ENABLED(BABYSTEPPING)
  1919. #if ENABLED(DELTA)
  1920. #define CYCLES_EATEN_BABYSTEP (2 * 15)
  1921. #else
  1922. #define CYCLES_EATEN_BABYSTEP 0
  1923. #endif
  1924. #define EXTRA_CYCLES_BABYSTEP (STEP_PULSE_CYCLES - (CYCLES_EATEN_BABYSTEP))
  1925. #define _ENABLE(AXIS) enable_## AXIS()
  1926. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  1927. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  1928. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  1929. #if EXTRA_CYCLES_BABYSTEP > 20
  1930. #define _SAVE_START const hal_timer_t pulse_start = HAL_timer_get_count(STEP_TIMER_NUM)
  1931. #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_count(STEP_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1932. #else
  1933. #define _SAVE_START NOOP
  1934. #if EXTRA_CYCLES_BABYSTEP > 0
  1935. #define _PULSE_WAIT DELAY_NOPS(EXTRA_CYCLES_BABYSTEP)
  1936. #elif STEP_PULSE_CYCLES > 0
  1937. #define _PULSE_WAIT NOOP
  1938. #elif ENABLED(DELTA)
  1939. #define _PULSE_WAIT delayMicroseconds(2);
  1940. #else
  1941. #define _PULSE_WAIT delayMicroseconds(4);
  1942. #endif
  1943. #endif
  1944. #define BABYSTEP_AXIS(AXIS, INVERT, DIR) { \
  1945. const uint8_t old_dir = _READ_DIR(AXIS); \
  1946. _ENABLE(AXIS); \
  1947. _SAVE_START; \
  1948. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^DIR^INVERT); \
  1949. _PULSE_WAIT; \
  1950. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  1951. _PULSE_WAIT; \
  1952. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  1953. _APPLY_DIR(AXIS, old_dir); \
  1954. }
  1955. // MUST ONLY BE CALLED BY AN ISR,
  1956. // No other ISR should ever interrupt this!
  1957. void Stepper::babystep(const AxisEnum axis, const bool direction) {
  1958. cli();
  1959. switch (axis) {
  1960. #if ENABLED(BABYSTEP_XY)
  1961. case X_AXIS:
  1962. #if CORE_IS_XY
  1963. BABYSTEP_AXIS(X, false, direction);
  1964. BABYSTEP_AXIS(Y, false, direction);
  1965. #elif CORE_IS_XZ
  1966. BABYSTEP_AXIS(X, false, direction);
  1967. BABYSTEP_AXIS(Z, false, direction);
  1968. #else
  1969. BABYSTEP_AXIS(X, false, direction);
  1970. #endif
  1971. break;
  1972. case Y_AXIS:
  1973. #if CORE_IS_XY
  1974. BABYSTEP_AXIS(X, false, direction);
  1975. BABYSTEP_AXIS(Y, false, direction^(CORESIGN(1)<0));
  1976. #elif CORE_IS_YZ
  1977. BABYSTEP_AXIS(Y, false, direction);
  1978. BABYSTEP_AXIS(Z, false, direction^(CORESIGN(1)<0));
  1979. #else
  1980. BABYSTEP_AXIS(Y, false, direction);
  1981. #endif
  1982. break;
  1983. #endif
  1984. case Z_AXIS: {
  1985. #if CORE_IS_XZ
  1986. BABYSTEP_AXIS(X, BABYSTEP_INVERT_Z, direction);
  1987. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
  1988. #elif CORE_IS_YZ
  1989. BABYSTEP_AXIS(Y, BABYSTEP_INVERT_Z, direction);
  1990. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
  1991. #elif DISABLED(DELTA)
  1992. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction);
  1993. #else // DELTA
  1994. const bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  1995. enable_X();
  1996. enable_Y();
  1997. enable_Z();
  1998. const uint8_t old_x_dir_pin = X_DIR_READ,
  1999. old_y_dir_pin = Y_DIR_READ,
  2000. old_z_dir_pin = Z_DIR_READ;
  2001. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  2002. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  2003. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  2004. _SAVE_START;
  2005. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  2006. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  2007. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  2008. _PULSE_WAIT;
  2009. X_STEP_WRITE(INVERT_X_STEP_PIN);
  2010. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  2011. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  2012. // Restore direction bits
  2013. X_DIR_WRITE(old_x_dir_pin);
  2014. Y_DIR_WRITE(old_y_dir_pin);
  2015. Z_DIR_WRITE(old_z_dir_pin);
  2016. #endif
  2017. } break;
  2018. default: break;
  2019. }
  2020. sei();
  2021. }
  2022. #endif // BABYSTEPPING
  2023. /**
  2024. * Software-controlled Stepper Motor Current
  2025. */
  2026. #if HAS_DIGIPOTSS
  2027. // From Arduino DigitalPotControl example
  2028. void Stepper::digitalPotWrite(const int16_t address, const int16_t value) {
  2029. WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
  2030. SPI.transfer(address); // Send the address and value via SPI
  2031. SPI.transfer(value);
  2032. WRITE(DIGIPOTSS_PIN, HIGH); // Take the SS pin high to de-select the chip
  2033. //delay(10);
  2034. }
  2035. #endif // HAS_DIGIPOTSS
  2036. #if HAS_MOTOR_CURRENT_PWM
  2037. void Stepper::refresh_motor_power() {
  2038. for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i) {
  2039. switch (i) {
  2040. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  2041. case 0:
  2042. #endif
  2043. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  2044. case 1:
  2045. #endif
  2046. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  2047. case 2:
  2048. #endif
  2049. digipot_current(i, motor_current_setting[i]);
  2050. default: break;
  2051. }
  2052. }
  2053. }
  2054. #endif // HAS_MOTOR_CURRENT_PWM
  2055. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  2056. void Stepper::digipot_current(const uint8_t driver, const int current) {
  2057. #if HAS_DIGIPOTSS
  2058. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  2059. digitalPotWrite(digipot_ch[driver], current);
  2060. #elif HAS_MOTOR_CURRENT_PWM
  2061. if (WITHIN(driver, 0, 2))
  2062. motor_current_setting[driver] = current; // update motor_current_setting
  2063. #define _WRITE_CURRENT_PWM(P) analogWrite(MOTOR_CURRENT_PWM_## P ##_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE))
  2064. switch (driver) {
  2065. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  2066. case 0: _WRITE_CURRENT_PWM(XY); break;
  2067. #endif
  2068. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  2069. case 1: _WRITE_CURRENT_PWM(Z); break;
  2070. #endif
  2071. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  2072. case 2: _WRITE_CURRENT_PWM(E); break;
  2073. #endif
  2074. }
  2075. #endif
  2076. }
  2077. void Stepper::digipot_init() {
  2078. #if HAS_DIGIPOTSS
  2079. static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  2080. SPI.begin();
  2081. SET_OUTPUT(DIGIPOTSS_PIN);
  2082. for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) {
  2083. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  2084. digipot_current(i, digipot_motor_current[i]);
  2085. }
  2086. #elif HAS_MOTOR_CURRENT_PWM
  2087. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  2088. SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN);
  2089. #endif
  2090. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  2091. SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN);
  2092. #endif
  2093. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  2094. SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN);
  2095. #endif
  2096. refresh_motor_power();
  2097. // Set Timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  2098. SET_CS5(PRESCALER_1);
  2099. #endif
  2100. }
  2101. #endif
  2102. #if HAS_MICROSTEPS
  2103. /**
  2104. * Software-controlled Microstepping
  2105. */
  2106. void Stepper::microstep_init() {
  2107. SET_OUTPUT(X_MS1_PIN);
  2108. SET_OUTPUT(X_MS2_PIN);
  2109. #if HAS_Y_MICROSTEPS
  2110. SET_OUTPUT(Y_MS1_PIN);
  2111. SET_OUTPUT(Y_MS2_PIN);
  2112. #endif
  2113. #if HAS_Z_MICROSTEPS
  2114. SET_OUTPUT(Z_MS1_PIN);
  2115. SET_OUTPUT(Z_MS2_PIN);
  2116. #endif
  2117. #if HAS_E0_MICROSTEPS
  2118. SET_OUTPUT(E0_MS1_PIN);
  2119. SET_OUTPUT(E0_MS2_PIN);
  2120. #endif
  2121. #if HAS_E1_MICROSTEPS
  2122. SET_OUTPUT(E1_MS1_PIN);
  2123. SET_OUTPUT(E1_MS2_PIN);
  2124. #endif
  2125. #if HAS_E2_MICROSTEPS
  2126. SET_OUTPUT(E2_MS1_PIN);
  2127. SET_OUTPUT(E2_MS2_PIN);
  2128. #endif
  2129. #if HAS_E3_MICROSTEPS
  2130. SET_OUTPUT(E3_MS1_PIN);
  2131. SET_OUTPUT(E3_MS2_PIN);
  2132. #endif
  2133. #if HAS_E4_MICROSTEPS
  2134. SET_OUTPUT(E4_MS1_PIN);
  2135. SET_OUTPUT(E4_MS2_PIN);
  2136. #endif
  2137. static const uint8_t microstep_modes[] = MICROSTEP_MODES;
  2138. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  2139. microstep_mode(i, microstep_modes[i]);
  2140. }
  2141. void Stepper::microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2) {
  2142. if (ms1 >= 0) switch (driver) {
  2143. case 0: WRITE(X_MS1_PIN, ms1); break;
  2144. #if HAS_Y_MICROSTEPS
  2145. case 1: WRITE(Y_MS1_PIN, ms1); break;
  2146. #endif
  2147. #if HAS_Z_MICROSTEPS
  2148. case 2: WRITE(Z_MS1_PIN, ms1); break;
  2149. #endif
  2150. #if HAS_E0_MICROSTEPS
  2151. case 3: WRITE(E0_MS1_PIN, ms1); break;
  2152. #endif
  2153. #if HAS_E1_MICROSTEPS
  2154. case 4: WRITE(E1_MS1_PIN, ms1); break;
  2155. #endif
  2156. #if HAS_E2_MICROSTEPS
  2157. case 5: WRITE(E2_MS1_PIN, ms1); break;
  2158. #endif
  2159. #if HAS_E3_MICROSTEPS
  2160. case 6: WRITE(E3_MS1_PIN, ms1); break;
  2161. #endif
  2162. #if HAS_E4_MICROSTEPS
  2163. case 7: WRITE(E4_MS1_PIN, ms1); break;
  2164. #endif
  2165. }
  2166. if (ms2 >= 0) switch (driver) {
  2167. case 0: WRITE(X_MS2_PIN, ms2); break;
  2168. #if HAS_Y_MICROSTEPS
  2169. case 1: WRITE(Y_MS2_PIN, ms2); break;
  2170. #endif
  2171. #if HAS_Z_MICROSTEPS
  2172. case 2: WRITE(Z_MS2_PIN, ms2); break;
  2173. #endif
  2174. #if HAS_E0_MICROSTEPS
  2175. case 3: WRITE(E0_MS2_PIN, ms2); break;
  2176. #endif
  2177. #if HAS_E1_MICROSTEPS
  2178. case 4: WRITE(E1_MS2_PIN, ms2); break;
  2179. #endif
  2180. #if HAS_E2_MICROSTEPS
  2181. case 5: WRITE(E2_MS2_PIN, ms2); break;
  2182. #endif
  2183. #if HAS_E3_MICROSTEPS
  2184. case 6: WRITE(E3_MS2_PIN, ms2); break;
  2185. #endif
  2186. #if HAS_E4_MICROSTEPS
  2187. case 7: WRITE(E4_MS2_PIN, ms2); break;
  2188. #endif
  2189. }
  2190. }
  2191. void Stepper::microstep_mode(const uint8_t driver, const uint8_t stepping_mode) {
  2192. switch (stepping_mode) {
  2193. case 1: microstep_ms(driver, MICROSTEP1); break;
  2194. #if ENABLED(HEROIC_STEPPER_DRIVERS)
  2195. case 128: microstep_ms(driver, MICROSTEP128); break;
  2196. #else
  2197. case 2: microstep_ms(driver, MICROSTEP2); break;
  2198. case 4: microstep_ms(driver, MICROSTEP4); break;
  2199. #endif
  2200. case 8: microstep_ms(driver, MICROSTEP8); break;
  2201. case 16: microstep_ms(driver, MICROSTEP16); break;
  2202. #if MB(ALLIGATOR)
  2203. case 32: microstep_ms(driver, MICROSTEP32); break;
  2204. #endif
  2205. default: SERIAL_ERROR_START(); SERIAL_ERRORLNPGM("Microsteps unavailable"); break;
  2206. }
  2207. }
  2208. void Stepper::microstep_readings() {
  2209. SERIAL_PROTOCOLLNPGM("MS1,MS2 Pins");
  2210. SERIAL_PROTOCOLPGM("X: ");
  2211. SERIAL_PROTOCOL(READ(X_MS1_PIN));
  2212. SERIAL_PROTOCOLLN(READ(X_MS2_PIN));
  2213. #if HAS_Y_MICROSTEPS
  2214. SERIAL_PROTOCOLPGM("Y: ");
  2215. SERIAL_PROTOCOL(READ(Y_MS1_PIN));
  2216. SERIAL_PROTOCOLLN(READ(Y_MS2_PIN));
  2217. #endif
  2218. #if HAS_Z_MICROSTEPS
  2219. SERIAL_PROTOCOLPGM("Z: ");
  2220. SERIAL_PROTOCOL(READ(Z_MS1_PIN));
  2221. SERIAL_PROTOCOLLN(READ(Z_MS2_PIN));
  2222. #endif
  2223. #if HAS_E0_MICROSTEPS
  2224. SERIAL_PROTOCOLPGM("E0: ");
  2225. SERIAL_PROTOCOL(READ(E0_MS1_PIN));
  2226. SERIAL_PROTOCOLLN(READ(E0_MS2_PIN));
  2227. #endif
  2228. #if HAS_E1_MICROSTEPS
  2229. SERIAL_PROTOCOLPGM("E1: ");
  2230. SERIAL_PROTOCOL(READ(E1_MS1_PIN));
  2231. SERIAL_PROTOCOLLN(READ(E1_MS2_PIN));
  2232. #endif
  2233. #if HAS_E2_MICROSTEPS
  2234. SERIAL_PROTOCOLPGM("E2: ");
  2235. SERIAL_PROTOCOL(READ(E2_MS1_PIN));
  2236. SERIAL_PROTOCOLLN(READ(E2_MS2_PIN));
  2237. #endif
  2238. #if HAS_E3_MICROSTEPS
  2239. SERIAL_PROTOCOLPGM("E3: ");
  2240. SERIAL_PROTOCOL(READ(E3_MS1_PIN));
  2241. SERIAL_PROTOCOLLN(READ(E3_MS2_PIN));
  2242. #endif
  2243. #if HAS_E4_MICROSTEPS
  2244. SERIAL_PROTOCOLPGM("E4: ");
  2245. SERIAL_PROTOCOL(READ(E4_MS1_PIN));
  2246. SERIAL_PROTOCOLLN(READ(E4_MS2_PIN));
  2247. #endif
  2248. }
  2249. #endif // HAS_MICROSTEPS