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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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 Stepper::bezier_A, // A coefficient in Bézier speed curve
  100. Stepper::bezier_B, // B coefficient in Bézier speed curve
  101. Stepper::bezier_C, // C coefficient in Bézier speed curve
  102. Stepper::bezier_F; // F coefficient in Bézier speed curve
  103. uint32_t Stepper::bezier_AV; // AV coefficient in Bézier speed curve
  104. bool Stepper::bezier_2nd_half; // =false If Bézier curve has been initialized or not
  105. #endif
  106. #if ENABLED(LIN_ADVANCE)
  107. uint32_t Stepper::LA_decelerate_after;
  108. constexpr hal_timer_t ADV_NEVER = HAL_TIMER_TYPE_MAX;
  109. hal_timer_t Stepper::nextMainISR = 0,
  110. Stepper::nextAdvanceISR = ADV_NEVER,
  111. Stepper::eISR_Rate = ADV_NEVER;
  112. uint16_t Stepper::current_adv_steps = 0,
  113. Stepper::final_adv_steps,
  114. Stepper::max_adv_steps;
  115. int8_t Stepper::e_steps = 0;
  116. #if E_STEPPERS > 1
  117. int8_t Stepper::LA_active_extruder; // Copy from current executed block. Needed because current_block is set to NULL "too early".
  118. #else
  119. constexpr int8_t Stepper::LA_active_extruder;
  120. #endif
  121. bool Stepper::use_advance_lead;
  122. #endif // LIN_ADVANCE
  123. int32_t Stepper::acceleration_time, Stepper::deceleration_time;
  124. volatile int32_t Stepper::count_position[NUM_AXIS] = { 0 };
  125. volatile signed char Stepper::count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
  126. #if ENABLED(MIXING_EXTRUDER)
  127. long Stepper::counter_m[MIXING_STEPPERS];
  128. #endif
  129. uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
  130. hal_timer_t Stepper::OCR1A_nominal;
  131. #if DISABLED(BEZIER_JERK_CONTROL)
  132. hal_timer_t Stepper::acc_step_rate; // needed for deceleration start point
  133. #endif
  134. volatile long Stepper::endstops_trigsteps[XYZ];
  135. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  136. #define LOCKED_X_MOTOR locked_x_motor
  137. #define LOCKED_Y_MOTOR locked_y_motor
  138. #define LOCKED_Z_MOTOR locked_z_motor
  139. #define LOCKED_X2_MOTOR locked_x2_motor
  140. #define LOCKED_Y2_MOTOR locked_y2_motor
  141. #define LOCKED_Z2_MOTOR locked_z2_motor
  142. #define DUAL_ENDSTOP_APPLY_STEP(AXIS,v) \
  143. if (performing_homing) { \
  144. if (AXIS##_HOME_DIR < 0) { \
  145. if (!(TEST(endstops.old_endstop_bits, AXIS##_MIN) && count_direction[AXIS##_AXIS] < 0) && !LOCKED_##AXIS##_MOTOR) AXIS##_STEP_WRITE(v); \
  146. if (!(TEST(endstops.old_endstop_bits, AXIS##2_MIN) && count_direction[AXIS##_AXIS] < 0) && !LOCKED_##AXIS##2_MOTOR) AXIS##2_STEP_WRITE(v); \
  147. } \
  148. else { \
  149. if (!(TEST(endstops.old_endstop_bits, AXIS##_MAX) && count_direction[AXIS##_AXIS] > 0) && !LOCKED_##AXIS##_MOTOR) AXIS##_STEP_WRITE(v); \
  150. if (!(TEST(endstops.old_endstop_bits, AXIS##2_MAX) && count_direction[AXIS##_AXIS] > 0) && !LOCKED_##AXIS##2_MOTOR) AXIS##2_STEP_WRITE(v); \
  151. } \
  152. } \
  153. else { \
  154. AXIS##_STEP_WRITE(v); \
  155. AXIS##2_STEP_WRITE(v); \
  156. }
  157. #endif
  158. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  159. #define X_APPLY_DIR(v,Q) do{ X_DIR_WRITE(v); X2_DIR_WRITE((v) != INVERT_X2_VS_X_DIR); }while(0)
  160. #if ENABLED(X_DUAL_ENDSTOPS)
  161. #define X_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(X,v)
  162. #else
  163. #define X_APPLY_STEP(v,Q) do{ X_STEP_WRITE(v); X2_STEP_WRITE(v); }while(0)
  164. #endif
  165. #elif ENABLED(DUAL_X_CARRIAGE)
  166. #define X_APPLY_DIR(v,ALWAYS) \
  167. if (extruder_duplication_enabled || ALWAYS) { \
  168. X_DIR_WRITE(v); \
  169. X2_DIR_WRITE(v); \
  170. } \
  171. else { \
  172. if (current_block->active_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  173. }
  174. #define X_APPLY_STEP(v,ALWAYS) \
  175. if (extruder_duplication_enabled || ALWAYS) { \
  176. X_STEP_WRITE(v); \
  177. X2_STEP_WRITE(v); \
  178. } \
  179. else { \
  180. if (current_block->active_extruder) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  181. }
  182. #else
  183. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  184. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  185. #endif
  186. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  187. #define Y_APPLY_DIR(v,Q) do{ Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }while(0)
  188. #if ENABLED(Y_DUAL_ENDSTOPS)
  189. #define Y_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Y,v)
  190. #else
  191. #define Y_APPLY_STEP(v,Q) do{ Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }while(0)
  192. #endif
  193. #else
  194. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  195. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  196. #endif
  197. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  198. #define Z_APPLY_DIR(v,Q) do{ Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }while(0)
  199. #if ENABLED(Z_DUAL_ENDSTOPS)
  200. #define Z_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Z,v)
  201. #else
  202. #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }while(0)
  203. #endif
  204. #else
  205. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  206. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  207. #endif
  208. #if DISABLED(MIXING_EXTRUDER)
  209. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
  210. #endif
  211. /**
  212. * __________________________
  213. * /| |\ _________________ ^
  214. * / | | \ /| |\ |
  215. * / | | \ / | | \ s
  216. * / | | | | | \ p
  217. * / | | | | | \ e
  218. * +-----+------------------------+---+--+---------------+----+ e
  219. * | BLOCK 1 | BLOCK 2 | d
  220. *
  221. * time ----->
  222. *
  223. * The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  224. * first block->accelerate_until step_events_completed, then keeps going at constant speed until
  225. * step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  226. * The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  227. */
  228. void Stepper::wake_up() {
  229. // TCNT1 = 0;
  230. ENABLE_STEPPER_DRIVER_INTERRUPT();
  231. }
  232. /**
  233. * Set the stepper direction of each axis
  234. *
  235. * COREXY: X_AXIS=A_AXIS and Y_AXIS=B_AXIS
  236. * COREXZ: X_AXIS=A_AXIS and Z_AXIS=C_AXIS
  237. * COREYZ: Y_AXIS=B_AXIS and Z_AXIS=C_AXIS
  238. */
  239. void Stepper::set_directions() {
  240. #define SET_STEP_DIR(AXIS) \
  241. if (motor_direction(AXIS ##_AXIS)) { \
  242. AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
  243. count_direction[AXIS ##_AXIS] = -1; \
  244. } \
  245. else { \
  246. AXIS ##_APPLY_DIR(!INVERT_## AXIS ##_DIR, false); \
  247. count_direction[AXIS ##_AXIS] = 1; \
  248. }
  249. #if HAS_X_DIR
  250. SET_STEP_DIR(X); // A
  251. #endif
  252. #if HAS_Y_DIR
  253. SET_STEP_DIR(Y); // B
  254. #endif
  255. #if HAS_Z_DIR
  256. SET_STEP_DIR(Z); // C
  257. #endif
  258. #if DISABLED(LIN_ADVANCE)
  259. if (motor_direction(E_AXIS)) {
  260. REV_E_DIR();
  261. count_direction[E_AXIS] = -1;
  262. }
  263. else {
  264. NORM_E_DIR();
  265. count_direction[E_AXIS] = 1;
  266. }
  267. #endif // !LIN_ADVANCE
  268. }
  269. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  270. extern volatile uint8_t e_hit;
  271. #endif
  272. #if ENABLED(BEZIER_JERK_CONTROL)
  273. /**
  274. * We are using a quintic (fifth-degree) Bézier polynomial for the velocity curve.
  275. * This gives us a "linear pop" velocity curve; with pop being the sixth derivative of position:
  276. * velocity - 1st, acceleration - 2nd, jerk - 3rd, snap - 4th, crackle - 5th, pop - 6th
  277. *
  278. * The Bézier curve takes the form:
  279. *
  280. * 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)
  281. *
  282. * Where 0 <= t <= 1, and V(t) is the velocity. P_0 through P_5 are the control points, and B_0(t)
  283. * through B_5(t) are the Bernstein basis as follows:
  284. *
  285. * B_0(t) = (1-t)^5 = -t^5 + 5t^4 - 10t^3 + 10t^2 - 5t + 1
  286. * B_1(t) = 5(1-t)^4 * t = 5t^5 - 20t^4 + 30t^3 - 20t^2 + 5t
  287. * B_2(t) = 10(1-t)^3 * t^2 = -10t^5 + 30t^4 - 30t^3 + 10t^2
  288. * B_3(t) = 10(1-t)^2 * t^3 = 10t^5 - 20t^4 + 10t^3
  289. * B_4(t) = 5(1-t) * t^4 = -5t^5 + 5t^4
  290. * B_5(t) = t^5 = t^5
  291. * ^ ^ ^ ^ ^ ^
  292. * | | | | | |
  293. * A B C D E F
  294. *
  295. * Unfortunately, we cannot use forward-differencing to calculate each position through
  296. * the curve, as Marlin uses variable timer periods. So, we require a formula of the form:
  297. *
  298. * V_f(t) = A*t^5 + B*t^4 + C*t^3 + D*t^2 + E*t + F
  299. *
  300. * Looking at the above B_0(t) through B_5(t) expanded forms, if we take the coefficients of t^5
  301. * through t of the Bézier form of V(t), we can determine that:
  302. *
  303. * A = -P_0 + 5*P_1 - 10*P_2 + 10*P_3 - 5*P_4 + P_5
  304. * B = 5*P_0 - 20*P_1 + 30*P_2 - 20*P_3 + 5*P_4
  305. * C = -10*P_0 + 30*P_1 - 30*P_2 + 10*P_3
  306. * D = 10*P_0 - 20*P_1 + 10*P_2
  307. * E = - 5*P_0 + 5*P_1
  308. * F = P_0
  309. *
  310. * Now, since we will (currently) *always* want the initial acceleration and jerk values to be 0,
  311. * We set P_i = P_0 = P_1 = P_2 (initial velocity), and P_t = P_3 = P_4 = P_5 (target velocity),
  312. * which, after simplification, resolves to:
  313. *
  314. * A = - 6*P_i + 6*P_t = 6*(P_t - P_i)
  315. * B = 15*P_i - 15*P_t = 15*(P_i - P_t)
  316. * C = -10*P_i + 10*P_t = 10*(P_t - P_i)
  317. * D = 0
  318. * E = 0
  319. * F = P_i
  320. *
  321. * As the t is evaluated in non uniform steps here, there is no other way rather than evaluating
  322. * the Bézier curve at each point:
  323. *
  324. * V_f(t) = A*t^5 + B*t^4 + C*t^3 + F [0 <= t <= 1]
  325. *
  326. * Floating point arithmetic execution time cost is prohibitive, so we will transform the math to
  327. * use fixed point values to be able to evaluate it in realtime. Assuming a maximum of 250000 steps
  328. * per second (driver pulses should at least be 2uS hi/2uS lo), and allocating 2 bits to avoid
  329. * overflows on the evaluation of the Bézier curve, means we can use
  330. *
  331. * t: unsigned Q0.32 (0 <= t < 1) |range 0 to 0xFFFFFFFF unsigned
  332. * A: signed Q24.7 , |range = +/- 250000 * 6 * 128 = +/- 192000000 = 0x0B71B000 | 28 bits + sign
  333. * B: signed Q24.7 , |range = +/- 250000 *15 * 128 = +/- 480000000 = 0x1C9C3800 | 29 bits + sign
  334. * C: signed Q24.7 , |range = +/- 250000 *10 * 128 = +/- 320000000 = 0x1312D000 | 29 bits + sign
  335. * F: signed Q24.7 , |range = +/- 250000 * 128 = 32000000 = 0x01E84800 | 25 bits + sign
  336. *
  337. * The trapezoid generator state contains the following information, that we will use to create and evaluate
  338. * the Bézier curve:
  339. *
  340. * blk->step_event_count [TS] = The total count of steps for this movement. (=distance)
  341. * blk->initial_rate [VI] = The initial steps per second (=velocity)
  342. * blk->final_rate [VF] = The ending steps per second (=velocity)
  343. * and the count of events completed (step_events_completed) [CS] (=distance until now)
  344. *
  345. * Note the abbreviations we use in the following formulae are between []s
  346. *
  347. * At the start of each trapezoid, we calculate the coefficients A,B,C,F and Advance [AV], as follows:
  348. *
  349. * A = 6*128*(VF - VI) = 768*(VF - VI)
  350. * B = 15*128*(VI - VF) = 1920*(VI - VF)
  351. * C = 10*128*(VF - VI) = 1280*(VF - VI)
  352. * F = 128*VI = 128*VI
  353. * AV = (1<<32)/TS ~= 0xFFFFFFFF / TS (To use ARM UDIV, that is 32 bits)
  354. *
  355. * And for each point, we will evaluate the curve with the following sequence:
  356. *
  357. * uint32_t t = bezier_AV * curr_step; // t: Range 0 - 1^32 = 32 bits
  358. * uint64_t f = t;
  359. * f *= t; // Range 32*2 = 64 bits (unsigned)
  360. * f >>= 32; // Range 32 bits (unsigned)
  361. * f *= t; // Range 32*2 = 64 bits (unsigned)
  362. * f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  363. * int64_t acc = (int64_t) bezier_F << 31; // Range 63 bits (signed)
  364. * acc += ((uint32_t) f >> 1) * (int64_t) bezier_C; // Range 29bits + 31 = 60bits (plus sign)
  365. * f *= t; // Range 32*2 = 64 bits
  366. * f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  367. * acc += ((uint32_t) f >> 1) * (int64_t) bezier_B; // Range 29bits + 31 = 60bits (plus sign)
  368. * f *= t; // Range 32*2 = 64 bits
  369. * f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  370. * acc += ((uint32_t) f >> 1) * (int64_t) bezier_A; // Range 28bits + 31 = 59bits (plus sign)
  371. * acc >>= (31 + 7); // Range 24bits (plus sign)
  372. *
  373. * This can be translated to the following ARM assembly sequence:
  374. *
  375. * At start:
  376. * fhi = AV, flo = CS, alo = F
  377. *
  378. * muls fhi,flo | f = AV * CS 1 cycles
  379. * mov t,fhi | t = AV * CS 1 cycles
  380. * lsrs ahi,alo,#1 | a = F << 31 1 cycles
  381. * lsls alo,alo,#31 | 1 cycles
  382. * umull flo,fhi,fhi,t | f *= t 5 cycles [fhi:flo=64bits
  383. * umull flo,fhi,fhi,t | f>>=32; f*=t 5 cycles [fhi:flo=64bits
  384. * lsrs flo,fhi,#1 | 1 cycles [31bits
  385. * smlal alo,ahi,flo,C | a+=(f>>33)*C; 5 cycles
  386. * umull flo,fhi,fhi,t | f>>=32; f*=t 5 cycles [fhi:flo=64bits
  387. * lsrs flo,fhi,#1 | 1 cycles [31bits
  388. * smlal alo,ahi,flo,B | a+=(f>>33)*B; 5 cycles
  389. * umull flo,fhi,fhi,t | f>>=32; f*=t 5 cycles [fhi:flo=64bits
  390. * lsrs flo,fhi,#1 | f>>=33; 1 cycles [31bits
  391. * smlal alo,ahi,flo,A | a+=(f>>33)*A; 5 cycles
  392. * lsrs alo,ahi,#6 | a>>=38 1 cycles
  393. * 43 cycles total
  394. */
  395. FORCE_INLINE void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t interval) {
  396. // Calculate the Bézier coefficients
  397. bezier_A = 768 * (v1 - v0);
  398. bezier_B = 1920 * (v0 - v1);
  399. bezier_C = 1280 * (v1 - v0);
  400. bezier_F = 128 * v0;
  401. bezier_AV = 0xFFFFFFFF / interval;
  402. }
  403. FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
  404. #if defined(__ARM__) || defined(__thumb__)
  405. // For ARM CORTEX M3/M4 CPUs, we have the optimized assembler version, that takes 43 cycles to execute
  406. register uint32_t flo = 0;
  407. register uint32_t fhi = bezier_AV * curr_step;
  408. register uint32_t t = fhi;
  409. register int32_t alo = bezier_F;
  410. register int32_t ahi = 0;
  411. register int32_t A = bezier_A;
  412. register int32_t B = bezier_B;
  413. register int32_t C = bezier_C;
  414. __asm__ __volatile__(
  415. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  416. " lsrs %[ahi],%[alo],#1" "\n\t" // a = F << 31 1 cycles
  417. " lsls %[alo],%[alo],#31" "\n\t" // 1 cycles
  418. " umull %[flo],%[fhi],%[fhi],%[t]" "\n\t" // f *= t 5 cycles [fhi:flo=64bits]
  419. " umull %[flo],%[fhi],%[fhi],%[t]" "\n\t" // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  420. " lsrs %[flo],%[fhi],#1" "\n\t" // 1 cycles [31bits]
  421. " smlal %[alo],%[ahi],%[flo],%[C]" "\n\t" // a+=(f>>33)*C; 5 cycles
  422. " umull %[flo],%[fhi],%[fhi],%[t]" "\n\t" // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  423. " lsrs %[flo],%[fhi],#1" "\n\t" // 1 cycles [31bits]
  424. " smlal %[alo],%[ahi],%[flo],%[B]" "\n\t" // a+=(f>>33)*B; 5 cycles
  425. " umull %[flo],%[fhi],%[fhi],%[t]" "\n\t" // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  426. " lsrs %[flo],%[fhi],#1" "\n\t" // f>>=33; 1 cycles [31bits]
  427. " smlal %[alo],%[ahi],%[flo],%[A]" "\n\t" // a+=(f>>33)*A; 5 cycles
  428. " lsrs %[alo],%[ahi],#6" "\n\t" // a>>=38 1 cycles
  429. : [alo]"+r"( alo ) ,
  430. [flo]"+r"( flo ) ,
  431. [fhi]"+r"( fhi ) ,
  432. [ahi]"+r"( ahi ) ,
  433. [A]"+r"( A ) , // <== Note: Even if A, B, C, and t registers are INPUT ONLY
  434. [B]"+r"( B ) , // GCC does bad optimizations on the code if we list them as
  435. [C]"+r"( C ) , // such, breaking this function. So, to avoid that problem,
  436. [t]"+r"( t ) // we list all registers as input-outputs.
  437. :
  438. : "cc"
  439. );
  440. return alo;
  441. #else
  442. // For non ARM targets, we provide a fallback implementation. Really doubt it
  443. // will be useful, unless the processor is extremely fast.
  444. uint32_t t = bezier_AV * curr_step; // t: Range 0 - 1^32 = 32 bits
  445. uint64_t f = t;
  446. f *= t; // Range 32*2 = 64 bits (unsigned)
  447. f >>= 32; // Range 32 bits (unsigned)
  448. f *= t; // Range 32*2 = 64 bits (unsigned)
  449. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  450. int64_t acc = (int64_t) bezier_F << 31; // Range 63 bits (signed)
  451. acc += ((uint32_t) f >> 1) * (int64_t) bezier_C; // Range 29bits + 31 = 60bits (plus sign)
  452. f *= t; // Range 32*2 = 64 bits
  453. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  454. acc += ((uint32_t) f >> 1) * (int64_t) bezier_B; // Range 29bits + 31 = 60bits (plus sign)
  455. f *= t; // Range 32*2 = 64 bits
  456. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  457. acc += ((uint32_t) f >> 1) * (int64_t) bezier_A; // Range 28bits + 31 = 59bits (plus sign)
  458. acc >>= (31 + 7); // Range 24bits (plus sign)
  459. return (int32_t) acc;
  460. #endif
  461. }
  462. #endif // BEZIER_JERK_CONTROL
  463. /**
  464. * Stepper Driver Interrupt
  465. *
  466. * Directly pulses the stepper motors at high frequency.
  467. *
  468. * AVR :
  469. * Timer 1 runs at a base frequency of 2MHz, with this ISR using OCR1A compare mode.
  470. *
  471. * OCR1A Frequency
  472. * 1 2 MHz
  473. * 50 40 KHz
  474. * 100 20 KHz - capped max rate
  475. * 200 10 KHz - nominal max rate
  476. * 2000 1 KHz - sleep rate
  477. * 4000 500 Hz - init rate
  478. */
  479. HAL_STEP_TIMER_ISR {
  480. HAL_timer_isr_prologue(STEP_TIMER_NUM);
  481. #if ENABLED(LIN_ADVANCE)
  482. Stepper::advance_isr_scheduler();
  483. #else
  484. Stepper::isr();
  485. #endif
  486. }
  487. void Stepper::isr() {
  488. #define ENDSTOP_NOMINAL_OCR_VAL 1500 * HAL_TICKS_PER_US // Check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch
  489. #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
  490. #if DISABLED(LIN_ADVANCE)
  491. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  492. DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
  493. DISABLE_STEPPER_DRIVER_INTERRUPT();
  494. #ifndef CPU_32_BIT
  495. sei();
  496. #endif
  497. #endif
  498. hal_timer_t ocr_val;
  499. static uint32_t step_remaining = 0; // SPLIT function always runs. This allows 16 bit timers to be
  500. // used to generate the stepper ISR.
  501. #define SPLIT(L) do { \
  502. if (L > ENDSTOP_NOMINAL_OCR_VAL) { \
  503. const uint32_t remainder = (uint32_t)L % (ENDSTOP_NOMINAL_OCR_VAL); \
  504. ocr_val = (remainder < OCR_VAL_TOLERANCE) ? ENDSTOP_NOMINAL_OCR_VAL + remainder : ENDSTOP_NOMINAL_OCR_VAL; \
  505. step_remaining = (uint32_t)L - ocr_val; \
  506. } \
  507. else \
  508. ocr_val = L;\
  509. }while(0)
  510. // Time remaining before the next step?
  511. if (step_remaining) {
  512. // Make sure endstops are updated
  513. if (ENDSTOPS_ENABLED) endstops.update();
  514. // Next ISR either for endstops or stepping
  515. ocr_val = step_remaining <= ENDSTOP_NOMINAL_OCR_VAL ? step_remaining : ENDSTOP_NOMINAL_OCR_VAL;
  516. step_remaining -= ocr_val;
  517. _NEXT_ISR(ocr_val);
  518. #if DISABLED(LIN_ADVANCE)
  519. HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
  520. HAL_ENABLE_ISRs();
  521. #endif
  522. return;
  523. }
  524. //
  525. // When cleaning, discard the current block and run fast
  526. //
  527. if (cleaning_buffer_counter) {
  528. if (cleaning_buffer_counter < 0) { // Count up for endstop hit
  529. if (current_block) planner.discard_current_block(); // Discard the active block that led to the trigger
  530. if (!planner.discard_continued_block()) // Discard next CONTINUED block
  531. cleaning_buffer_counter = 0; // Keep discarding until non-CONTINUED
  532. }
  533. else {
  534. planner.discard_current_block();
  535. --cleaning_buffer_counter; // Count down for abort print
  536. #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
  537. if (!cleaning_buffer_counter) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  538. #endif
  539. }
  540. current_block = NULL; // Prep to get a new block after cleaning
  541. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 10000); // Run at max speed - 10 KHz
  542. HAL_ENABLE_ISRs();
  543. return;
  544. }
  545. // If there is no current block, attempt to pop one from the buffer
  546. if (!current_block) {
  547. // Anything in the buffer?
  548. if ((current_block = planner.get_current_block())) {
  549. // Initialize the trapezoid generator from the current block.
  550. static int8_t last_extruder = -1;
  551. #if ENABLED(LIN_ADVANCE)
  552. #if E_STEPPERS > 1
  553. if (current_block->active_extruder != last_extruder) {
  554. current_adv_steps = 0; // If the now active extruder wasn't in use during the last move, its pressure is most likely gone.
  555. LA_active_extruder = current_block->active_extruder;
  556. }
  557. #endif
  558. if ((use_advance_lead = current_block->use_advance_lead)) {
  559. LA_decelerate_after = current_block->decelerate_after;
  560. final_adv_steps = current_block->final_adv_steps;
  561. max_adv_steps = current_block->max_adv_steps;
  562. }
  563. #endif
  564. if (current_block->direction_bits != last_direction_bits || current_block->active_extruder != last_extruder) {
  565. last_direction_bits = current_block->direction_bits;
  566. last_extruder = current_block->active_extruder;
  567. set_directions();
  568. }
  569. // No acceleration / deceleration time elapsed so far
  570. acceleration_time = deceleration_time = 0;
  571. // No step events completed so far
  572. step_events_completed = 0;
  573. // step_rate to timer interval
  574. OCR1A_nominal = calc_timer_interval(current_block->nominal_rate);
  575. // make a note of the number of step loops required at nominal speed
  576. step_loops_nominal = step_loops;
  577. #if DISABLED(BEZIER_JERK_CONTROL)
  578. // Set as deceleration point the initial rate of the block
  579. acc_step_rate = current_block->initial_rate;
  580. #endif
  581. #if ENABLED(BEZIER_JERK_CONTROL)
  582. // Initialize the Bézier speed curve
  583. _calc_bezier_curve_coeffs(current_block->initial_rate, current_block->cruise_rate, current_block->acceleration_time);
  584. // We have not started the 2nd half of the trapezoid
  585. bezier_2nd_half = false;
  586. #endif
  587. // Initialize Bresenham counters to 1/2 the ceiling
  588. counter_X = counter_Y = counter_Z = counter_E = -(current_block->step_event_count >> 1);
  589. #if ENABLED(MIXING_EXTRUDER)
  590. MIXING_STEPPERS_LOOP(i)
  591. counter_m[i] = -(current_block->mix_event_count[i] >> 1);
  592. #endif
  593. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  594. e_hit = 2; // Needed for the case an endstop is already triggered before the new move begins.
  595. // No 'change' can be detected.
  596. #endif
  597. #if ENABLED(Z_LATE_ENABLE)
  598. // If delayed Z enable, postpone move for 1mS
  599. if (current_block->steps[Z_AXIS] > 0) {
  600. enable_Z();
  601. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
  602. HAL_ENABLE_ISRs();
  603. return;
  604. }
  605. #endif
  606. }
  607. else {
  608. // If no more queued moves, postpone next check for 1mS
  609. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
  610. HAL_ENABLE_ISRs();
  611. return;
  612. }
  613. }
  614. // Update endstops state, if enabled
  615. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  616. if (e_hit && ENDSTOPS_ENABLED) {
  617. endstops.update();
  618. e_hit--;
  619. }
  620. #else
  621. if (ENDSTOPS_ENABLED) endstops.update();
  622. #endif
  623. // Take multiple steps per interrupt (For high speed moves)
  624. bool all_steps_done = false;
  625. for (uint8_t i = step_loops; i--;) {
  626. #define _COUNTER(AXIS) counter_## AXIS
  627. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  628. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  629. // Advance the Bresenham counter; start a pulse if the axis needs a step
  630. #define PULSE_START(AXIS) do{ \
  631. _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
  632. if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); } \
  633. }while(0)
  634. // Advance the Bresenham counter; start a pulse if the axis needs a step
  635. #define STEP_TICK(AXIS) do { \
  636. if (_COUNTER(AXIS) > 0) { \
  637. _COUNTER(AXIS) -= current_block->step_event_count; \
  638. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  639. } \
  640. }while(0)
  641. // Stop an active pulse, if any
  642. #define PULSE_STOP(AXIS) _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), 0)
  643. /**
  644. * Estimate the number of cycles that the stepper logic already takes
  645. * up between the start and stop of the X stepper pulse.
  646. *
  647. * Currently this uses very modest estimates of around 5 cycles.
  648. * True values may be derived by careful testing.
  649. *
  650. * Once any delay is added, the cost of the delay code itself
  651. * may be subtracted from this value to get a more accurate delay.
  652. * Delays under 20 cycles (1.25µs) will be very accurate, using NOPs.
  653. * Longer delays use a loop. The resolution is 8 cycles.
  654. */
  655. #if HAS_X_STEP
  656. #define _CYCLE_APPROX_1 5
  657. #else
  658. #define _CYCLE_APPROX_1 0
  659. #endif
  660. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  661. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1 + 4
  662. #else
  663. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1
  664. #endif
  665. #if HAS_Y_STEP
  666. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2 + 5
  667. #else
  668. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2
  669. #endif
  670. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  671. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3 + 4
  672. #else
  673. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3
  674. #endif
  675. #if HAS_Z_STEP
  676. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4 + 5
  677. #else
  678. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4
  679. #endif
  680. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  681. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5 + 4
  682. #else
  683. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5
  684. #endif
  685. #if DISABLED(LIN_ADVANCE)
  686. #if ENABLED(MIXING_EXTRUDER)
  687. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + (MIXING_STEPPERS) * 6
  688. #else
  689. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + 5
  690. #endif
  691. #else
  692. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6
  693. #endif
  694. #define CYCLES_EATEN_XYZE _CYCLE_APPROX_7
  695. #define EXTRA_CYCLES_XYZE (STEP_PULSE_CYCLES - (CYCLES_EATEN_XYZE))
  696. /**
  697. * If a minimum pulse time was specified get the timer 0 value.
  698. *
  699. * On AVR the TCNT0 timer has an 8x prescaler, so it increments every 8 cycles.
  700. * That's every 0.5µs on 16MHz and every 0.4µs on 20MHz.
  701. * 20 counts of TCNT0 -by itself- is a good pulse delay.
  702. * 10µs = 160 or 200 cycles.
  703. */
  704. #if EXTRA_CYCLES_XYZE > 20
  705. hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  706. #endif
  707. #if HAS_X_STEP
  708. PULSE_START(X);
  709. #endif
  710. #if HAS_Y_STEP
  711. PULSE_START(Y);
  712. #endif
  713. #if HAS_Z_STEP
  714. PULSE_START(Z);
  715. #endif
  716. #if ENABLED(LIN_ADVANCE)
  717. counter_E += current_block->steps[E_AXIS];
  718. if (counter_E > 0) {
  719. #if DISABLED(MIXING_EXTRUDER)
  720. // Don't step E here for mixing extruder
  721. motor_direction(E_AXIS) ? --e_steps : ++e_steps;
  722. #endif
  723. }
  724. #if ENABLED(MIXING_EXTRUDER)
  725. // Step mixing steppers proportionally
  726. const bool dir = motor_direction(E_AXIS);
  727. MIXING_STEPPERS_LOOP(j) {
  728. counter_m[j] += current_block->steps[E_AXIS];
  729. if (counter_m[j] > 0) {
  730. counter_m[j] -= current_block->mix_event_count[j];
  731. dir ? --e_steps[j] : ++e_steps[j];
  732. }
  733. }
  734. #endif
  735. #else // !LIN_ADVANCE - use linear interpolation for E also
  736. #if ENABLED(MIXING_EXTRUDER)
  737. // Keep updating the single E axis
  738. counter_E += current_block->steps[E_AXIS];
  739. // Tick the counters used for this mix
  740. MIXING_STEPPERS_LOOP(j) {
  741. // Step mixing steppers (proportionally)
  742. counter_m[j] += current_block->steps[E_AXIS];
  743. // Step when the counter goes over zero
  744. if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
  745. }
  746. #else // !MIXING_EXTRUDER
  747. PULSE_START(E);
  748. #endif
  749. #endif // !LIN_ADVANCE
  750. #if HAS_X_STEP
  751. STEP_TICK(X);
  752. #endif
  753. #if HAS_Y_STEP
  754. STEP_TICK(Y);
  755. #endif
  756. #if HAS_Z_STEP
  757. STEP_TICK(Z);
  758. #endif
  759. STEP_TICK(E); // Always tick the single E axis
  760. // For minimum pulse time wait before stopping pulses
  761. #if EXTRA_CYCLES_XYZE > 20
  762. while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  763. pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  764. #elif EXTRA_CYCLES_XYZE > 0
  765. DELAY_NOPS(EXTRA_CYCLES_XYZE);
  766. #endif
  767. #if HAS_X_STEP
  768. PULSE_STOP(X);
  769. #endif
  770. #if HAS_Y_STEP
  771. PULSE_STOP(Y);
  772. #endif
  773. #if HAS_Z_STEP
  774. PULSE_STOP(Z);
  775. #endif
  776. #if DISABLED(LIN_ADVANCE)
  777. #if ENABLED(MIXING_EXTRUDER)
  778. MIXING_STEPPERS_LOOP(j) {
  779. if (counter_m[j] > 0) {
  780. counter_m[j] -= current_block->mix_event_count[j];
  781. En_STEP_WRITE(j, INVERT_E_STEP_PIN);
  782. }
  783. }
  784. #else // !MIXING_EXTRUDER
  785. PULSE_STOP(E);
  786. #endif
  787. #endif // !LIN_ADVANCE
  788. if (++step_events_completed >= current_block->step_event_count) {
  789. all_steps_done = true;
  790. break;
  791. }
  792. // For minimum pulse time wait after stopping pulses also
  793. #if EXTRA_CYCLES_XYZE > 20
  794. if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  795. #elif EXTRA_CYCLES_XYZE > 0
  796. if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE);
  797. #endif
  798. } // steps_loop
  799. // Calculate new timer value
  800. if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
  801. #if ENABLED(BEZIER_JERK_CONTROL)
  802. // Get the next speed to use (Jerk limited!)
  803. hal_timer_t acc_step_rate =
  804. acceleration_time < current_block->acceleration_time
  805. ? _eval_bezier_curve(acceleration_time)
  806. : current_block->cruise_rate;
  807. #else
  808. #ifdef CPU_32_BIT
  809. MultiU32X24toH32(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  810. #else
  811. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  812. #endif
  813. acc_step_rate += current_block->initial_rate;
  814. // upper limit
  815. NOMORE(acc_step_rate, current_block->nominal_rate);
  816. #endif
  817. // step_rate to timer interval
  818. const hal_timer_t interval = calc_timer_interval(acc_step_rate);
  819. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  820. _NEXT_ISR(ocr_val);
  821. acceleration_time += interval;
  822. #if ENABLED(LIN_ADVANCE)
  823. if (current_block->use_advance_lead) {
  824. if (step_events_completed == step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  825. nextAdvanceISR = 0; // Wake up eISR on first acceleration loop and fire ISR if final adv_rate is reached
  826. eISR_Rate = current_block->advance_speed;
  827. }
  828. }
  829. else {
  830. eISR_Rate = ADV_NEVER;
  831. if (e_steps) nextAdvanceISR = 0;
  832. }
  833. #endif // LIN_ADVANCE
  834. }
  835. else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
  836. hal_timer_t step_rate;
  837. #if ENABLED(BEZIER_JERK_CONTROL)
  838. // If this is the 1st time we process the 2nd half of the trapezoid...
  839. if (!bezier_2nd_half) {
  840. // Initialize the Bézier speed curve
  841. _calc_bezier_curve_coeffs(current_block->cruise_rate, current_block->final_rate, current_block->deceleration_time);
  842. bezier_2nd_half = true;
  843. }
  844. // Calculate the next speed to use
  845. step_rate = deceleration_time < current_block->deceleration_time
  846. ? _eval_bezier_curve(deceleration_time)
  847. : current_block->final_rate;
  848. #else
  849. // Using the old trapezoidal control
  850. #ifdef CPU_32_BIT
  851. MultiU32X24toH32(step_rate, deceleration_time, current_block->acceleration_rate);
  852. #else
  853. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  854. #endif
  855. if (step_rate < acc_step_rate) { // Still decelerating?
  856. step_rate = acc_step_rate - step_rate;
  857. NOLESS(step_rate, current_block->final_rate);
  858. }
  859. else
  860. step_rate = current_block->final_rate;
  861. #endif
  862. // step_rate to timer interval
  863. const hal_timer_t interval = calc_timer_interval(step_rate);
  864. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  865. _NEXT_ISR(ocr_val);
  866. deceleration_time += interval;
  867. #if ENABLED(LIN_ADVANCE)
  868. if (current_block->use_advance_lead) {
  869. if (step_events_completed <= (uint32_t)current_block->decelerate_after + step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  870. nextAdvanceISR = 0; // Wake up eISR on first deceleration loop
  871. eISR_Rate = current_block->advance_speed;
  872. }
  873. }
  874. else {
  875. eISR_Rate = ADV_NEVER;
  876. if (e_steps) nextAdvanceISR = 0;
  877. }
  878. #endif // LIN_ADVANCE
  879. }
  880. else {
  881. #if ENABLED(LIN_ADVANCE)
  882. // If we have esteps to execute, fire the next advance_isr "now"
  883. if (e_steps && eISR_Rate != current_block->advance_speed) nextAdvanceISR = 0;
  884. #endif
  885. SPLIT(OCR1A_nominal); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  886. _NEXT_ISR(ocr_val);
  887. // ensure we're running at the correct step rate, even if we just came off an acceleration
  888. step_loops = step_loops_nominal;
  889. }
  890. #if DISABLED(LIN_ADVANCE)
  891. // Make sure stepper ISR doesn't monopolize the CPU
  892. HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
  893. #endif
  894. // If current block is finished, reset pointer
  895. if (all_steps_done) {
  896. current_block = NULL;
  897. planner.discard_current_block();
  898. }
  899. #if DISABLED(LIN_ADVANCE)
  900. HAL_ENABLE_ISRs();
  901. #endif
  902. }
  903. #if ENABLED(LIN_ADVANCE)
  904. #define CYCLES_EATEN_E (E_STEPPERS * 5)
  905. #define EXTRA_CYCLES_E (STEP_PULSE_CYCLES - (CYCLES_EATEN_E))
  906. // Timer interrupt for E. e_steps is set in the main routine;
  907. void Stepper::advance_isr() {
  908. #if ENABLED(MK2_MULTIPLEXER) // For SNMM even-numbered steppers are reversed
  909. #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)
  910. #elif ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  911. #define SET_E_STEP_DIR(INDEX) do{ if (e_steps) { if (e_steps < 0) REV_E_DIR(); else NORM_E_DIR(); } }while(0)
  912. #else
  913. #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)
  914. #endif
  915. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  916. #define START_E_PULSE(INDEX) do{ if (e_steps) E_STEP_WRITE(!INVERT_E_STEP_PIN); }while(0)
  917. #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)
  918. #else
  919. #define START_E_PULSE(INDEX) do{ if (e_steps) E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); }while(0)
  920. #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)
  921. #endif
  922. if (use_advance_lead) {
  923. if (step_events_completed > LA_decelerate_after && current_adv_steps > final_adv_steps) {
  924. e_steps--;
  925. current_adv_steps--;
  926. nextAdvanceISR = eISR_Rate;
  927. }
  928. else if (step_events_completed < LA_decelerate_after && current_adv_steps < max_adv_steps) {
  929. //step_events_completed <= (uint32_t)current_block->accelerate_until) {
  930. e_steps++;
  931. current_adv_steps++;
  932. nextAdvanceISR = eISR_Rate;
  933. }
  934. else {
  935. nextAdvanceISR = ADV_NEVER;
  936. eISR_Rate = ADV_NEVER;
  937. }
  938. }
  939. else
  940. nextAdvanceISR = ADV_NEVER;
  941. switch (LA_active_extruder) {
  942. case 0: SET_E_STEP_DIR(0); break;
  943. #if EXTRUDERS > 1
  944. case 1: SET_E_STEP_DIR(1); break;
  945. #if EXTRUDERS > 2
  946. case 2: SET_E_STEP_DIR(2); break;
  947. #if EXTRUDERS > 3
  948. case 3: SET_E_STEP_DIR(3); break;
  949. #if EXTRUDERS > 4
  950. case 4: SET_E_STEP_DIR(4); break;
  951. #endif // EXTRUDERS > 4
  952. #endif // EXTRUDERS > 3
  953. #endif // EXTRUDERS > 2
  954. #endif // EXTRUDERS > 1
  955. }
  956. // Step E stepper if we have steps
  957. while (e_steps) {
  958. #if EXTRA_CYCLES_E > 20
  959. hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  960. #endif
  961. switch (LA_active_extruder) {
  962. case 0: START_E_PULSE(0); break;
  963. #if EXTRUDERS > 1
  964. case 1: START_E_PULSE(1); break;
  965. #if EXTRUDERS > 2
  966. case 2: START_E_PULSE(2); break;
  967. #if EXTRUDERS > 3
  968. case 3: START_E_PULSE(3); break;
  969. #if EXTRUDERS > 4
  970. case 4: START_E_PULSE(4); break;
  971. #endif // EXTRUDERS > 4
  972. #endif // EXTRUDERS > 3
  973. #endif // EXTRUDERS > 2
  974. #endif // EXTRUDERS > 1
  975. }
  976. // For minimum pulse time wait before stopping pulses
  977. #if EXTRA_CYCLES_E > 20
  978. while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  979. pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  980. #elif EXTRA_CYCLES_E > 0
  981. DELAY_NOPS(EXTRA_CYCLES_E);
  982. #endif
  983. switch (LA_active_extruder) {
  984. case 0: STOP_E_PULSE(0); break;
  985. #if EXTRUDERS > 1
  986. case 1: STOP_E_PULSE(1); break;
  987. #if EXTRUDERS > 2
  988. case 2: STOP_E_PULSE(2); break;
  989. #if EXTRUDERS > 3
  990. case 3: STOP_E_PULSE(3); break;
  991. #if EXTRUDERS > 4
  992. case 4: STOP_E_PULSE(4); break;
  993. #endif // EXTRUDERS > 4
  994. #endif // EXTRUDERS > 3
  995. #endif // EXTRUDERS > 2
  996. #endif // EXTRUDERS > 1
  997. }
  998. // For minimum pulse time wait before looping
  999. #if EXTRA_CYCLES_E > 20
  1000. if (e_steps) while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1001. #elif EXTRA_CYCLES_E > 0
  1002. if (e_steps) DELAY_NOPS(EXTRA_CYCLES_E);
  1003. #endif
  1004. } // e_steps
  1005. }
  1006. void Stepper::advance_isr_scheduler() {
  1007. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  1008. DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
  1009. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1010. sei();
  1011. // Run main stepping ISR if flagged
  1012. if (!nextMainISR) isr();
  1013. // Run Advance stepping ISR if flagged
  1014. if (!nextAdvanceISR) advance_isr();
  1015. // Is the next advance ISR scheduled before the next main ISR?
  1016. if (nextAdvanceISR <= nextMainISR) {
  1017. // Set up the next interrupt
  1018. HAL_timer_set_compare(STEP_TIMER_NUM, nextAdvanceISR);
  1019. // New interval for the next main ISR
  1020. if (nextMainISR) nextMainISR -= nextAdvanceISR;
  1021. // Will call Stepper::advance_isr on the next interrupt
  1022. nextAdvanceISR = 0;
  1023. }
  1024. else {
  1025. // The next main ISR comes first
  1026. HAL_timer_set_compare(STEP_TIMER_NUM, nextMainISR);
  1027. // New interval for the next advance ISR, if any
  1028. if (nextAdvanceISR && nextAdvanceISR != ADV_NEVER)
  1029. nextAdvanceISR -= nextMainISR;
  1030. // Will call Stepper::isr on the next interrupt
  1031. nextMainISR = 0;
  1032. }
  1033. // Make sure stepper ISR doesn't monopolize the CPU
  1034. HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
  1035. // Restore original ISR settings
  1036. HAL_ENABLE_ISRs();
  1037. }
  1038. #endif // LIN_ADVANCE
  1039. void Stepper::init() {
  1040. // Init Digipot Motor Current
  1041. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  1042. digipot_init();
  1043. #endif
  1044. #if MB(ALLIGATOR)
  1045. const float motor_current[] = MOTOR_CURRENT;
  1046. unsigned int digipot_motor = 0;
  1047. for (uint8_t i = 0; i < 3 + EXTRUDERS; i++) {
  1048. digipot_motor = 255 * (motor_current[i] / 2.5);
  1049. dac084s085::setValue(i, digipot_motor);
  1050. }
  1051. #endif//MB(ALLIGATOR)
  1052. // Init Microstepping Pins
  1053. #if HAS_MICROSTEPS
  1054. microstep_init();
  1055. #endif
  1056. // Init Dir Pins
  1057. #if HAS_X_DIR
  1058. X_DIR_INIT;
  1059. #endif
  1060. #if HAS_X2_DIR
  1061. X2_DIR_INIT;
  1062. #endif
  1063. #if HAS_Y_DIR
  1064. Y_DIR_INIT;
  1065. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  1066. Y2_DIR_INIT;
  1067. #endif
  1068. #endif
  1069. #if HAS_Z_DIR
  1070. Z_DIR_INIT;
  1071. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  1072. Z2_DIR_INIT;
  1073. #endif
  1074. #endif
  1075. #if HAS_E0_DIR
  1076. E0_DIR_INIT;
  1077. #endif
  1078. #if HAS_E1_DIR
  1079. E1_DIR_INIT;
  1080. #endif
  1081. #if HAS_E2_DIR
  1082. E2_DIR_INIT;
  1083. #endif
  1084. #if HAS_E3_DIR
  1085. E3_DIR_INIT;
  1086. #endif
  1087. #if HAS_E4_DIR
  1088. E4_DIR_INIT;
  1089. #endif
  1090. // Init Enable Pins - steppers default to disabled.
  1091. #if HAS_X_ENABLE
  1092. X_ENABLE_INIT;
  1093. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  1094. #if (ENABLED(DUAL_X_CARRIAGE) || ENABLED(X_DUAL_STEPPER_DRIVERS)) && HAS_X2_ENABLE
  1095. X2_ENABLE_INIT;
  1096. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  1097. #endif
  1098. #endif
  1099. #if HAS_Y_ENABLE
  1100. Y_ENABLE_INIT;
  1101. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  1102. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  1103. Y2_ENABLE_INIT;
  1104. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  1105. #endif
  1106. #endif
  1107. #if HAS_Z_ENABLE
  1108. Z_ENABLE_INIT;
  1109. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  1110. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  1111. Z2_ENABLE_INIT;
  1112. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  1113. #endif
  1114. #endif
  1115. #if HAS_E0_ENABLE
  1116. E0_ENABLE_INIT;
  1117. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  1118. #endif
  1119. #if HAS_E1_ENABLE
  1120. E1_ENABLE_INIT;
  1121. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  1122. #endif
  1123. #if HAS_E2_ENABLE
  1124. E2_ENABLE_INIT;
  1125. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  1126. #endif
  1127. #if HAS_E3_ENABLE
  1128. E3_ENABLE_INIT;
  1129. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  1130. #endif
  1131. #if HAS_E4_ENABLE
  1132. E4_ENABLE_INIT;
  1133. if (!E_ENABLE_ON) E4_ENABLE_WRITE(HIGH);
  1134. #endif
  1135. // Init endstops and pullups
  1136. endstops.init();
  1137. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  1138. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  1139. #define _DISABLE(AXIS) disable_## AXIS()
  1140. #define AXIS_INIT(AXIS, PIN) \
  1141. _STEP_INIT(AXIS); \
  1142. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  1143. _DISABLE(AXIS)
  1144. #define E_AXIS_INIT(NUM) AXIS_INIT(E## NUM, E)
  1145. // Init Step Pins
  1146. #if HAS_X_STEP
  1147. #if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)
  1148. X2_STEP_INIT;
  1149. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  1150. #endif
  1151. AXIS_INIT(X, X);
  1152. #endif
  1153. #if HAS_Y_STEP
  1154. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  1155. Y2_STEP_INIT;
  1156. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  1157. #endif
  1158. AXIS_INIT(Y, Y);
  1159. #endif
  1160. #if HAS_Z_STEP
  1161. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  1162. Z2_STEP_INIT;
  1163. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  1164. #endif
  1165. AXIS_INIT(Z, Z);
  1166. #endif
  1167. #if HAS_E0_STEP
  1168. E_AXIS_INIT(0);
  1169. #endif
  1170. #if HAS_E1_STEP
  1171. E_AXIS_INIT(1);
  1172. #endif
  1173. #if HAS_E2_STEP
  1174. E_AXIS_INIT(2);
  1175. #endif
  1176. #if HAS_E3_STEP
  1177. E_AXIS_INIT(3);
  1178. #endif
  1179. #if HAS_E4_STEP
  1180. E_AXIS_INIT(4);
  1181. #endif
  1182. #ifdef __AVR__
  1183. // waveform generation = 0100 = CTC
  1184. SET_WGM(1, CTC_OCRnA);
  1185. // output mode = 00 (disconnected)
  1186. SET_COMA(1, NORMAL);
  1187. // Set the timer pre-scaler
  1188. // Generally we use a divider of 8, resulting in a 2MHz timer
  1189. // frequency on a 16MHz MCU. If you are going to change this, be
  1190. // sure to regenerate speed_lookuptable.h with
  1191. // create_speed_lookuptable.py
  1192. SET_CS(1, PRESCALER_8); // CS 2 = 1/8 prescaler
  1193. // Init Stepper ISR to 122 Hz for quick starting
  1194. OCR1A = 0x4000;
  1195. TCNT1 = 0;
  1196. #else
  1197. // Init Stepper ISR to 122 Hz for quick starting
  1198. HAL_timer_start(STEP_TIMER_NUM, 122);
  1199. #endif
  1200. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1201. endstops.enable(true); // Start with endstops active. After homing they can be disabled
  1202. sei();
  1203. set_directions(); // Init directions to last_direction_bits = 0
  1204. }
  1205. /**
  1206. * Block until all buffered steps are executed / cleaned
  1207. */
  1208. void Stepper::synchronize() { while (planner.has_blocks_queued() || cleaning_buffer_counter) idle(); }
  1209. /**
  1210. * Set the stepper positions directly in steps
  1211. *
  1212. * The input is based on the typical per-axis XYZ steps.
  1213. * For CORE machines XYZ needs to be translated to ABC.
  1214. *
  1215. * This allows get_axis_position_mm to correctly
  1216. * derive the current XYZ position later on.
  1217. */
  1218. void Stepper::set_position(const long &a, const long &b, const long &c, const long &e) {
  1219. synchronize(); // Bad to set stepper counts in the middle of a move
  1220. CRITICAL_SECTION_START;
  1221. #if CORE_IS_XY
  1222. // corexy positioning
  1223. // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
  1224. count_position[A_AXIS] = a + b;
  1225. count_position[B_AXIS] = CORESIGN(a - b);
  1226. count_position[Z_AXIS] = c;
  1227. #elif CORE_IS_XZ
  1228. // corexz planning
  1229. count_position[A_AXIS] = a + c;
  1230. count_position[Y_AXIS] = b;
  1231. count_position[C_AXIS] = CORESIGN(a - c);
  1232. #elif CORE_IS_YZ
  1233. // coreyz planning
  1234. count_position[X_AXIS] = a;
  1235. count_position[B_AXIS] = b + c;
  1236. count_position[C_AXIS] = CORESIGN(b - c);
  1237. #else
  1238. // default non-h-bot planning
  1239. count_position[X_AXIS] = a;
  1240. count_position[Y_AXIS] = b;
  1241. count_position[Z_AXIS] = c;
  1242. #endif
  1243. count_position[E_AXIS] = e;
  1244. CRITICAL_SECTION_END;
  1245. }
  1246. void Stepper::set_position(const AxisEnum &axis, const long &v) {
  1247. CRITICAL_SECTION_START;
  1248. count_position[axis] = v;
  1249. CRITICAL_SECTION_END;
  1250. }
  1251. void Stepper::set_e_position(const long &e) {
  1252. CRITICAL_SECTION_START;
  1253. count_position[E_AXIS] = e;
  1254. CRITICAL_SECTION_END;
  1255. }
  1256. /**
  1257. * Get a stepper's position in steps.
  1258. */
  1259. long Stepper::position(const AxisEnum axis) {
  1260. CRITICAL_SECTION_START;
  1261. const long count_pos = count_position[axis];
  1262. CRITICAL_SECTION_END;
  1263. return count_pos;
  1264. }
  1265. /**
  1266. * Get an axis position according to stepper position(s)
  1267. * For CORE machines apply translation from ABC to XYZ.
  1268. */
  1269. float Stepper::get_axis_position_mm(const AxisEnum axis) {
  1270. float axis_steps;
  1271. #if IS_CORE
  1272. // Requesting one of the "core" axes?
  1273. if (axis == CORE_AXIS_1 || axis == CORE_AXIS_2) {
  1274. CRITICAL_SECTION_START;
  1275. // ((a1+a2)+(a1-a2))/2 -> (a1+a2+a1-a2)/2 -> (a1+a1)/2 -> a1
  1276. // ((a1+a2)-(a1-a2))/2 -> (a1+a2-a1+a2)/2 -> (a2+a2)/2 -> a2
  1277. axis_steps = 0.5f * (
  1278. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1279. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1280. );
  1281. CRITICAL_SECTION_END;
  1282. }
  1283. else
  1284. axis_steps = position(axis);
  1285. #else
  1286. axis_steps = position(axis);
  1287. #endif
  1288. return axis_steps * planner.steps_to_mm[axis];
  1289. }
  1290. void Stepper::finish_and_disable() {
  1291. synchronize();
  1292. disable_all_steppers();
  1293. }
  1294. void Stepper::quick_stop() {
  1295. cleaning_buffer_counter = 5000;
  1296. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1297. while (planner.has_blocks_queued()) planner.discard_current_block();
  1298. current_block = NULL;
  1299. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1300. #if ENABLED(ULTRA_LCD)
  1301. planner.clear_block_buffer_runtime();
  1302. #endif
  1303. }
  1304. void Stepper::endstop_triggered(const AxisEnum axis) {
  1305. #if IS_CORE
  1306. endstops_trigsteps[axis] = 0.5f * (
  1307. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1308. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1309. );
  1310. #else // !COREXY && !COREXZ && !COREYZ
  1311. endstops_trigsteps[axis] = count_position[axis];
  1312. #endif // !COREXY && !COREXZ && !COREYZ
  1313. kill_current_block();
  1314. cleaning_buffer_counter = -1; // Discard the rest of the move
  1315. }
  1316. void Stepper::report_positions() {
  1317. CRITICAL_SECTION_START;
  1318. const long xpos = count_position[X_AXIS],
  1319. ypos = count_position[Y_AXIS],
  1320. zpos = count_position[Z_AXIS];
  1321. CRITICAL_SECTION_END;
  1322. #if CORE_IS_XY || CORE_IS_XZ || IS_DELTA || IS_SCARA
  1323. SERIAL_PROTOCOLPGM(MSG_COUNT_A);
  1324. #else
  1325. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1326. #endif
  1327. SERIAL_PROTOCOL(xpos);
  1328. #if CORE_IS_XY || CORE_IS_YZ || IS_DELTA || IS_SCARA
  1329. SERIAL_PROTOCOLPGM(" B:");
  1330. #else
  1331. SERIAL_PROTOCOLPGM(" Y:");
  1332. #endif
  1333. SERIAL_PROTOCOL(ypos);
  1334. #if CORE_IS_XZ || CORE_IS_YZ || IS_DELTA
  1335. SERIAL_PROTOCOLPGM(" C:");
  1336. #else
  1337. SERIAL_PROTOCOLPGM(" Z:");
  1338. #endif
  1339. SERIAL_PROTOCOL(zpos);
  1340. SERIAL_EOL();
  1341. }
  1342. #if ENABLED(BABYSTEPPING)
  1343. #if ENABLED(DELTA)
  1344. #define CYCLES_EATEN_BABYSTEP (2 * 15)
  1345. #else
  1346. #define CYCLES_EATEN_BABYSTEP 0
  1347. #endif
  1348. #define EXTRA_CYCLES_BABYSTEP (STEP_PULSE_CYCLES - (CYCLES_EATEN_BABYSTEP))
  1349. #define _ENABLE(AXIS) enable_## AXIS()
  1350. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  1351. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  1352. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  1353. #if EXTRA_CYCLES_BABYSTEP > 20
  1354. #define _SAVE_START const hal_timer_t pulse_start = HAL_timer_get_count(STEP_TIMER_NUM)
  1355. #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_count(STEP_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1356. #else
  1357. #define _SAVE_START NOOP
  1358. #if EXTRA_CYCLES_BABYSTEP > 0
  1359. #define _PULSE_WAIT DELAY_NOPS(EXTRA_CYCLES_BABYSTEP)
  1360. #elif STEP_PULSE_CYCLES > 0
  1361. #define _PULSE_WAIT NOOP
  1362. #elif ENABLED(DELTA)
  1363. #define _PULSE_WAIT delayMicroseconds(2);
  1364. #else
  1365. #define _PULSE_WAIT delayMicroseconds(4);
  1366. #endif
  1367. #endif
  1368. #define BABYSTEP_AXIS(AXIS, INVERT, DIR) { \
  1369. const uint8_t old_dir = _READ_DIR(AXIS); \
  1370. _ENABLE(AXIS); \
  1371. _SAVE_START; \
  1372. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^DIR^INVERT); \
  1373. _PULSE_WAIT; \
  1374. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  1375. _PULSE_WAIT; \
  1376. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  1377. _APPLY_DIR(AXIS, old_dir); \
  1378. }
  1379. // MUST ONLY BE CALLED BY AN ISR,
  1380. // No other ISR should ever interrupt this!
  1381. void Stepper::babystep(const AxisEnum axis, const bool direction) {
  1382. cli();
  1383. switch (axis) {
  1384. #if ENABLED(BABYSTEP_XY)
  1385. case X_AXIS:
  1386. #if CORE_IS_XY
  1387. BABYSTEP_AXIS(X, false, direction);
  1388. BABYSTEP_AXIS(Y, false, direction);
  1389. #elif CORE_IS_XZ
  1390. BABYSTEP_AXIS(X, false, direction);
  1391. BABYSTEP_AXIS(Z, false, direction);
  1392. #else
  1393. BABYSTEP_AXIS(X, false, direction);
  1394. #endif
  1395. break;
  1396. case Y_AXIS:
  1397. #if CORE_IS_XY
  1398. BABYSTEP_AXIS(X, false, direction);
  1399. BABYSTEP_AXIS(Y, false, direction^(CORESIGN(1)<0));
  1400. #elif CORE_IS_YZ
  1401. BABYSTEP_AXIS(Y, false, direction);
  1402. BABYSTEP_AXIS(Z, false, direction^(CORESIGN(1)<0));
  1403. #else
  1404. BABYSTEP_AXIS(Y, false, direction);
  1405. #endif
  1406. break;
  1407. #endif
  1408. case Z_AXIS: {
  1409. #if CORE_IS_XZ
  1410. BABYSTEP_AXIS(X, BABYSTEP_INVERT_Z, direction);
  1411. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
  1412. #elif CORE_IS_YZ
  1413. BABYSTEP_AXIS(Y, BABYSTEP_INVERT_Z, direction);
  1414. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
  1415. #elif DISABLED(DELTA)
  1416. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction);
  1417. #else // DELTA
  1418. const bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  1419. enable_X();
  1420. enable_Y();
  1421. enable_Z();
  1422. const uint8_t old_x_dir_pin = X_DIR_READ,
  1423. old_y_dir_pin = Y_DIR_READ,
  1424. old_z_dir_pin = Z_DIR_READ;
  1425. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  1426. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  1427. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  1428. _SAVE_START;
  1429. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  1430. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1431. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1432. _PULSE_WAIT;
  1433. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1434. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1435. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  1436. // Restore direction bits
  1437. X_DIR_WRITE(old_x_dir_pin);
  1438. Y_DIR_WRITE(old_y_dir_pin);
  1439. Z_DIR_WRITE(old_z_dir_pin);
  1440. #endif
  1441. } break;
  1442. default: break;
  1443. }
  1444. sei();
  1445. }
  1446. #endif // BABYSTEPPING
  1447. /**
  1448. * Software-controlled Stepper Motor Current
  1449. */
  1450. #if HAS_DIGIPOTSS
  1451. // From Arduino DigitalPotControl example
  1452. void Stepper::digitalPotWrite(const int16_t address, const int16_t value) {
  1453. WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
  1454. SPI.transfer(address); // Send the address and value via SPI
  1455. SPI.transfer(value);
  1456. WRITE(DIGIPOTSS_PIN, HIGH); // Take the SS pin high to de-select the chip
  1457. //delay(10);
  1458. }
  1459. #endif // HAS_DIGIPOTSS
  1460. #if HAS_MOTOR_CURRENT_PWM
  1461. void Stepper::refresh_motor_power() {
  1462. for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i) {
  1463. switch (i) {
  1464. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1465. case 0:
  1466. #endif
  1467. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1468. case 1:
  1469. #endif
  1470. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1471. case 2:
  1472. #endif
  1473. digipot_current(i, motor_current_setting[i]);
  1474. default: break;
  1475. }
  1476. }
  1477. }
  1478. #endif // HAS_MOTOR_CURRENT_PWM
  1479. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  1480. void Stepper::digipot_current(const uint8_t driver, const int current) {
  1481. #if HAS_DIGIPOTSS
  1482. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1483. digitalPotWrite(digipot_ch[driver], current);
  1484. #elif HAS_MOTOR_CURRENT_PWM
  1485. if (WITHIN(driver, 0, 2))
  1486. motor_current_setting[driver] = current; // update motor_current_setting
  1487. #define _WRITE_CURRENT_PWM(P) analogWrite(MOTOR_CURRENT_PWM_## P ##_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE))
  1488. switch (driver) {
  1489. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1490. case 0: _WRITE_CURRENT_PWM(XY); break;
  1491. #endif
  1492. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1493. case 1: _WRITE_CURRENT_PWM(Z); break;
  1494. #endif
  1495. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1496. case 2: _WRITE_CURRENT_PWM(E); break;
  1497. #endif
  1498. }
  1499. #endif
  1500. }
  1501. void Stepper::digipot_init() {
  1502. #if HAS_DIGIPOTSS
  1503. static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1504. SPI.begin();
  1505. SET_OUTPUT(DIGIPOTSS_PIN);
  1506. for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) {
  1507. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1508. digipot_current(i, digipot_motor_current[i]);
  1509. }
  1510. #elif HAS_MOTOR_CURRENT_PWM
  1511. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1512. SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN);
  1513. #endif
  1514. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1515. SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN);
  1516. #endif
  1517. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1518. SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN);
  1519. #endif
  1520. refresh_motor_power();
  1521. // Set Timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1522. SET_CS5(PRESCALER_1);
  1523. #endif
  1524. }
  1525. #endif
  1526. #if HAS_MICROSTEPS
  1527. /**
  1528. * Software-controlled Microstepping
  1529. */
  1530. void Stepper::microstep_init() {
  1531. SET_OUTPUT(X_MS1_PIN);
  1532. SET_OUTPUT(X_MS2_PIN);
  1533. #if HAS_Y_MICROSTEPS
  1534. SET_OUTPUT(Y_MS1_PIN);
  1535. SET_OUTPUT(Y_MS2_PIN);
  1536. #endif
  1537. #if HAS_Z_MICROSTEPS
  1538. SET_OUTPUT(Z_MS1_PIN);
  1539. SET_OUTPUT(Z_MS2_PIN);
  1540. #endif
  1541. #if HAS_E0_MICROSTEPS
  1542. SET_OUTPUT(E0_MS1_PIN);
  1543. SET_OUTPUT(E0_MS2_PIN);
  1544. #endif
  1545. #if HAS_E1_MICROSTEPS
  1546. SET_OUTPUT(E1_MS1_PIN);
  1547. SET_OUTPUT(E1_MS2_PIN);
  1548. #endif
  1549. #if HAS_E2_MICROSTEPS
  1550. SET_OUTPUT(E2_MS1_PIN);
  1551. SET_OUTPUT(E2_MS2_PIN);
  1552. #endif
  1553. #if HAS_E3_MICROSTEPS
  1554. SET_OUTPUT(E3_MS1_PIN);
  1555. SET_OUTPUT(E3_MS2_PIN);
  1556. #endif
  1557. #if HAS_E4_MICROSTEPS
  1558. SET_OUTPUT(E4_MS1_PIN);
  1559. SET_OUTPUT(E4_MS2_PIN);
  1560. #endif
  1561. static const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1562. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  1563. microstep_mode(i, microstep_modes[i]);
  1564. }
  1565. void Stepper::microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2) {
  1566. if (ms1 >= 0) switch (driver) {
  1567. case 0: WRITE(X_MS1_PIN, ms1); break;
  1568. #if HAS_Y_MICROSTEPS
  1569. case 1: WRITE(Y_MS1_PIN, ms1); break;
  1570. #endif
  1571. #if HAS_Z_MICROSTEPS
  1572. case 2: WRITE(Z_MS1_PIN, ms1); break;
  1573. #endif
  1574. #if HAS_E0_MICROSTEPS
  1575. case 3: WRITE(E0_MS1_PIN, ms1); break;
  1576. #endif
  1577. #if HAS_E1_MICROSTEPS
  1578. case 4: WRITE(E1_MS1_PIN, ms1); break;
  1579. #endif
  1580. #if HAS_E2_MICROSTEPS
  1581. case 5: WRITE(E2_MS1_PIN, ms1); break;
  1582. #endif
  1583. #if HAS_E3_MICROSTEPS
  1584. case 6: WRITE(E3_MS1_PIN, ms1); break;
  1585. #endif
  1586. #if HAS_E4_MICROSTEPS
  1587. case 7: WRITE(E4_MS1_PIN, ms1); break;
  1588. #endif
  1589. }
  1590. if (ms2 >= 0) switch (driver) {
  1591. case 0: WRITE(X_MS2_PIN, ms2); break;
  1592. #if HAS_Y_MICROSTEPS
  1593. case 1: WRITE(Y_MS2_PIN, ms2); break;
  1594. #endif
  1595. #if HAS_Z_MICROSTEPS
  1596. case 2: WRITE(Z_MS2_PIN, ms2); break;
  1597. #endif
  1598. #if HAS_E0_MICROSTEPS
  1599. case 3: WRITE(E0_MS2_PIN, ms2); break;
  1600. #endif
  1601. #if HAS_E1_MICROSTEPS
  1602. case 4: WRITE(E1_MS2_PIN, ms2); break;
  1603. #endif
  1604. #if HAS_E2_MICROSTEPS
  1605. case 5: WRITE(E2_MS2_PIN, ms2); break;
  1606. #endif
  1607. #if HAS_E3_MICROSTEPS
  1608. case 6: WRITE(E3_MS2_PIN, ms2); break;
  1609. #endif
  1610. #if HAS_E4_MICROSTEPS
  1611. case 7: WRITE(E4_MS2_PIN, ms2); break;
  1612. #endif
  1613. }
  1614. }
  1615. void Stepper::microstep_mode(const uint8_t driver, const uint8_t stepping_mode) {
  1616. switch (stepping_mode) {
  1617. case 1: microstep_ms(driver, MICROSTEP1); break;
  1618. #if ENABLED(HEROIC_STEPPER_DRIVERS)
  1619. case 128: microstep_ms(driver, MICROSTEP128); break;
  1620. #else
  1621. case 2: microstep_ms(driver, MICROSTEP2); break;
  1622. case 4: microstep_ms(driver, MICROSTEP4); break;
  1623. #endif
  1624. case 8: microstep_ms(driver, MICROSTEP8); break;
  1625. case 16: microstep_ms(driver, MICROSTEP16); break;
  1626. #if MB(ALLIGATOR)
  1627. case 32: microstep_ms(driver, MICROSTEP32); break;
  1628. #endif
  1629. default: SERIAL_ERROR_START(); SERIAL_ERRORLNPGM("Microsteps unavailable"); break;
  1630. }
  1631. }
  1632. void Stepper::microstep_readings() {
  1633. SERIAL_PROTOCOLLNPGM("MS1,MS2 Pins");
  1634. SERIAL_PROTOCOLPGM("X: ");
  1635. SERIAL_PROTOCOL(READ(X_MS1_PIN));
  1636. SERIAL_PROTOCOLLN(READ(X_MS2_PIN));
  1637. #if HAS_Y_MICROSTEPS
  1638. SERIAL_PROTOCOLPGM("Y: ");
  1639. SERIAL_PROTOCOL(READ(Y_MS1_PIN));
  1640. SERIAL_PROTOCOLLN(READ(Y_MS2_PIN));
  1641. #endif
  1642. #if HAS_Z_MICROSTEPS
  1643. SERIAL_PROTOCOLPGM("Z: ");
  1644. SERIAL_PROTOCOL(READ(Z_MS1_PIN));
  1645. SERIAL_PROTOCOLLN(READ(Z_MS2_PIN));
  1646. #endif
  1647. #if HAS_E0_MICROSTEPS
  1648. SERIAL_PROTOCOLPGM("E0: ");
  1649. SERIAL_PROTOCOL(READ(E0_MS1_PIN));
  1650. SERIAL_PROTOCOLLN(READ(E0_MS2_PIN));
  1651. #endif
  1652. #if HAS_E1_MICROSTEPS
  1653. SERIAL_PROTOCOLPGM("E1: ");
  1654. SERIAL_PROTOCOL(READ(E1_MS1_PIN));
  1655. SERIAL_PROTOCOLLN(READ(E1_MS2_PIN));
  1656. #endif
  1657. #if HAS_E2_MICROSTEPS
  1658. SERIAL_PROTOCOLPGM("E2: ");
  1659. SERIAL_PROTOCOL(READ(E2_MS1_PIN));
  1660. SERIAL_PROTOCOLLN(READ(E2_MS2_PIN));
  1661. #endif
  1662. #if HAS_E3_MICROSTEPS
  1663. SERIAL_PROTOCOLPGM("E3: ");
  1664. SERIAL_PROTOCOL(READ(E3_MS1_PIN));
  1665. SERIAL_PROTOCOLLN(READ(E3_MS2_PIN));
  1666. #endif
  1667. #if HAS_E4_MICROSTEPS
  1668. SERIAL_PROTOCOLPGM("E4: ");
  1669. SERIAL_PROTOCOL(READ(E4_MS1_PIN));
  1670. SERIAL_PROTOCOLLN(READ(E4_MS2_PIN));
  1671. #endif
  1672. }
  1673. #endif // HAS_MICROSTEPS