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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /**
  2. * stepper.cpp - stepper motor driver: executes motion plans using stepper motors
  3. * Marlin Firmware
  4. *
  5. * Derived from Grbl
  6. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  7. *
  8. * Grbl 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. * Grbl 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 Grbl. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
  22. and Philipp Tiefenbacher. */
  23. #include "Marlin.h"
  24. #include "stepper.h"
  25. #include "planner.h"
  26. #include "temperature.h"
  27. #include "ultralcd.h"
  28. #include "language.h"
  29. #include "cardreader.h"
  30. #include "speed_lookuptable.h"
  31. #include "dac_mcp4728.h"
  32. #if HAS_DIGIPOTSS
  33. #include <SPI.h>
  34. #endif
  35. //===========================================================================
  36. //============================= public variables ============================
  37. //===========================================================================
  38. block_t* current_block; // A pointer to the block currently being traced
  39. //===========================================================================
  40. //============================= private variables ===========================
  41. //===========================================================================
  42. //static makes it impossible to be called from outside of this file by extern.!
  43. // Variables used by The Stepper Driver Interrupt
  44. static unsigned char out_bits = 0; // The next stepping-bits to be output
  45. static unsigned int cleaning_buffer_counter;
  46. #if ENABLED(Z_DUAL_ENDSTOPS)
  47. static bool performing_homing = false,
  48. locked_z_motor = false,
  49. locked_z2_motor = false;
  50. #endif
  51. // Counter variables for the Bresenham line tracer
  52. static long counter_x, counter_y, counter_z, counter_e;
  53. volatile static unsigned long step_events_completed; // The number of step events executed in the current block
  54. #if ENABLED(ADVANCE)
  55. static long advance_rate, advance, final_advance = 0;
  56. static long old_advance = 0;
  57. static long e_steps[4];
  58. #endif
  59. static long acceleration_time, deceleration_time;
  60. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  61. static unsigned short acc_step_rate; // needed for deceleration start point
  62. static uint8_t step_loops;
  63. static uint8_t step_loops_nominal;
  64. static unsigned short OCR1A_nominal;
  65. volatile long endstops_trigsteps[3] = { 0 };
  66. volatile long endstops_stepsTotal, endstops_stepsDone;
  67. static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
  68. #if DISABLED(Z_DUAL_ENDSTOPS)
  69. static byte
  70. #else
  71. static uint16_t
  72. #endif
  73. old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_MIN_PROBE, Z2_MIN, Z2_MAX
  74. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  75. bool abort_on_endstop_hit = false;
  76. #endif
  77. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  78. #ifndef PWM_MOTOR_CURRENT
  79. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  80. #endif
  81. const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
  82. #endif
  83. static bool check_endstops = true;
  84. volatile long count_position[NUM_AXIS] = { 0 }; // Positions of stepper motors, in step units
  85. volatile signed char count_direction[NUM_AXIS] = { 1 };
  86. //===========================================================================
  87. //================================ functions ================================
  88. //===========================================================================
  89. #if ENABLED(DUAL_X_CARRIAGE)
  90. #define X_APPLY_DIR(v,ALWAYS) \
  91. if (extruder_duplication_enabled || ALWAYS) { \
  92. X_DIR_WRITE(v); \
  93. X2_DIR_WRITE(v); \
  94. } \
  95. else { \
  96. if (current_block->active_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  97. }
  98. #define X_APPLY_STEP(v,ALWAYS) \
  99. if (extruder_duplication_enabled || ALWAYS) { \
  100. X_STEP_WRITE(v); \
  101. X2_STEP_WRITE(v); \
  102. } \
  103. else { \
  104. if (current_block->active_extruder != 0) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  105. }
  106. #else
  107. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  108. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  109. #endif
  110. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  111. #define Y_APPLY_DIR(v,Q) { Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }
  112. #define Y_APPLY_STEP(v,Q) { Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }
  113. #else
  114. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  115. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  116. #endif
  117. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  118. #define Z_APPLY_DIR(v,Q) { Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }
  119. #if ENABLED(Z_DUAL_ENDSTOPS)
  120. #define Z_APPLY_STEP(v,Q) \
  121. if (performing_homing) { \
  122. if (Z_HOME_DIR > 0) {\
  123. if (!(TEST(old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  124. if (!(TEST(old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  125. } \
  126. else { \
  127. if (!(TEST(old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  128. if (!(TEST(old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  129. } \
  130. } \
  131. else { \
  132. Z_STEP_WRITE(v); \
  133. Z2_STEP_WRITE(v); \
  134. }
  135. #else
  136. #define Z_APPLY_STEP(v,Q) { Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }
  137. #endif
  138. #else
  139. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  140. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  141. #endif
  142. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
  143. // intRes = intIn1 * intIn2 >> 16
  144. // uses:
  145. // r26 to store 0
  146. // r27 to store the byte 1 of the 24 bit result
  147. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  148. asm volatile ( \
  149. "clr r26 \n\t" \
  150. "mul %A1, %B2 \n\t" \
  151. "movw %A0, r0 \n\t" \
  152. "mul %A1, %A2 \n\t" \
  153. "add %A0, r1 \n\t" \
  154. "adc %B0, r26 \n\t" \
  155. "lsr r0 \n\t" \
  156. "adc %A0, r26 \n\t" \
  157. "adc %B0, r26 \n\t" \
  158. "clr r1 \n\t" \
  159. : \
  160. "=&r" (intRes) \
  161. : \
  162. "d" (charIn1), \
  163. "d" (intIn2) \
  164. : \
  165. "r26" \
  166. )
  167. // intRes = longIn1 * longIn2 >> 24
  168. // uses:
  169. // r26 to store 0
  170. // r27 to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
  171. // note that the lower two bytes and the upper byte of the 48bit result are not calculated.
  172. // this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
  173. // B0 A0 are bits 24-39 and are the returned value
  174. // C1 B1 A1 is longIn1
  175. // D2 C2 B2 A2 is longIn2
  176. //
  177. #define MultiU24X32toH16(intRes, longIn1, longIn2) \
  178. asm volatile ( \
  179. "clr r26 \n\t" \
  180. "mul %A1, %B2 \n\t" \
  181. "mov r27, r1 \n\t" \
  182. "mul %B1, %C2 \n\t" \
  183. "movw %A0, r0 \n\t" \
  184. "mul %C1, %C2 \n\t" \
  185. "add %B0, r0 \n\t" \
  186. "mul %C1, %B2 \n\t" \
  187. "add %A0, r0 \n\t" \
  188. "adc %B0, r1 \n\t" \
  189. "mul %A1, %C2 \n\t" \
  190. "add r27, r0 \n\t" \
  191. "adc %A0, r1 \n\t" \
  192. "adc %B0, r26 \n\t" \
  193. "mul %B1, %B2 \n\t" \
  194. "add r27, r0 \n\t" \
  195. "adc %A0, r1 \n\t" \
  196. "adc %B0, r26 \n\t" \
  197. "mul %C1, %A2 \n\t" \
  198. "add r27, r0 \n\t" \
  199. "adc %A0, r1 \n\t" \
  200. "adc %B0, r26 \n\t" \
  201. "mul %B1, %A2 \n\t" \
  202. "add r27, r1 \n\t" \
  203. "adc %A0, r26 \n\t" \
  204. "adc %B0, r26 \n\t" \
  205. "lsr r27 \n\t" \
  206. "adc %A0, r26 \n\t" \
  207. "adc %B0, r26 \n\t" \
  208. "mul %D2, %A1 \n\t" \
  209. "add %A0, r0 \n\t" \
  210. "adc %B0, r1 \n\t" \
  211. "mul %D2, %B1 \n\t" \
  212. "add %B0, r0 \n\t" \
  213. "clr r1 \n\t" \
  214. : \
  215. "=&r" (intRes) \
  216. : \
  217. "d" (longIn1), \
  218. "d" (longIn2) \
  219. : \
  220. "r26" , "r27" \
  221. )
  222. // Some useful constants
  223. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  224. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  225. void endstops_hit_on_purpose() {
  226. endstop_hit_bits = 0;
  227. }
  228. void checkHitEndstops() {
  229. if (endstop_hit_bits) {
  230. SERIAL_ECHO_START;
  231. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  232. if (TEST(endstop_hit_bits, X_MIN)) {
  233. SERIAL_ECHOPAIR(" X:", (float)endstops_trigsteps[X_AXIS] / axis_steps_per_unit[X_AXIS]);
  234. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "X");
  235. }
  236. if (TEST(endstop_hit_bits, Y_MIN)) {
  237. SERIAL_ECHOPAIR(" Y:", (float)endstops_trigsteps[Y_AXIS] / axis_steps_per_unit[Y_AXIS]);
  238. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Y");
  239. }
  240. if (TEST(endstop_hit_bits, Z_MIN)) {
  241. SERIAL_ECHOPAIR(" Z:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
  242. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
  243. }
  244. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  245. if (TEST(endstop_hit_bits, Z_MIN_PROBE)) {
  246. SERIAL_ECHOPAIR(" Z_MIN_PROBE:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
  247. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "ZP");
  248. }
  249. #endif
  250. SERIAL_EOL;
  251. endstops_hit_on_purpose();
  252. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  253. if (abort_on_endstop_hit) {
  254. card.sdprinting = false;
  255. card.closefile();
  256. quickStop();
  257. disable_all_heaters(); // switch off all heaters.
  258. }
  259. #endif
  260. }
  261. }
  262. void enable_endstops(bool check) { check_endstops = check; }
  263. // Check endstops - Called from ISR!
  264. inline void update_endstops() {
  265. #if ENABLED(Z_DUAL_ENDSTOPS)
  266. uint16_t
  267. #else
  268. byte
  269. #endif
  270. current_endstop_bits = 0;
  271. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  272. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  273. #define _AXIS(AXIS) AXIS ##_AXIS
  274. #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
  275. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  276. // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  277. #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  278. // COPY_BIT: copy the value of COPY_BIT to BIT in bits
  279. #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
  280. // TEST_ENDSTOP: test the old and the current status of an endstop
  281. #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
  282. #if ENABLED(COREXY) || ENABLED(COREXZ)
  283. #define _SET_TRIGSTEPS(AXIS) do { \
  284. float axis_pos = count_position[_AXIS(AXIS)]; \
  285. if (_AXIS(AXIS) == A_AXIS) \
  286. axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2; \
  287. else if (_AXIS(AXIS) == CORE_AXIS_2) \
  288. axis_pos = (count_position[A_AXIS] - axis_pos) / 2; \
  289. endstops_trigsteps[_AXIS(AXIS)] = axis_pos; \
  290. } while(0)
  291. #else
  292. #define _SET_TRIGSTEPS(AXIS) endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]
  293. #endif // COREXY || COREXZ
  294. #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
  295. SET_ENDSTOP_BIT(AXIS, MINMAX); \
  296. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && current_block->steps[_AXIS(AXIS)] > 0) { \
  297. _SET_TRIGSTEPS(AXIS); \
  298. _ENDSTOP_HIT(AXIS); \
  299. step_events_completed = current_block->step_event_count; \
  300. } \
  301. } while(0)
  302. #if ENABLED(COREXY) || ENABLED(COREXZ)
  303. // Head direction in -X axis for CoreXY and CoreXZ bots.
  304. // If Delta1 == -Delta2, the movement is only in Y or Z axis
  305. if ((current_block->steps[A_AXIS] != current_block->steps[CORE_AXIS_2]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, CORE_AXIS_2))) {
  306. if (TEST(out_bits, X_HEAD))
  307. #else
  308. if (TEST(out_bits, X_AXIS)) // stepping along -X axis (regular Cartesian bot)
  309. #endif
  310. { // -direction
  311. #if ENABLED(DUAL_X_CARRIAGE)
  312. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  313. if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
  314. #endif
  315. {
  316. #if HAS_X_MIN
  317. UPDATE_ENDSTOP(X, MIN);
  318. #endif
  319. }
  320. }
  321. else { // +direction
  322. #if ENABLED(DUAL_X_CARRIAGE)
  323. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  324. if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
  325. #endif
  326. {
  327. #if HAS_X_MAX
  328. UPDATE_ENDSTOP(X, MAX);
  329. #endif
  330. }
  331. }
  332. #if ENABLED(COREXY) || ENABLED(COREXZ)
  333. }
  334. #endif
  335. #if ENABLED(COREXY)
  336. // Head direction in -Y axis for CoreXY bots.
  337. // If DeltaX == DeltaY, the movement is only in X axis
  338. if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, B_AXIS))) {
  339. if (TEST(out_bits, Y_HEAD))
  340. #else
  341. if (TEST(out_bits, Y_AXIS)) // -direction
  342. #endif
  343. { // -direction
  344. #if HAS_Y_MIN
  345. UPDATE_ENDSTOP(Y, MIN);
  346. #endif
  347. }
  348. else { // +direction
  349. #if HAS_Y_MAX
  350. UPDATE_ENDSTOP(Y, MAX);
  351. #endif
  352. }
  353. #if ENABLED(COREXY)
  354. }
  355. #endif
  356. #if ENABLED(COREXZ)
  357. // Head direction in -Z axis for CoreXZ bots.
  358. // If DeltaX == DeltaZ, the movement is only in X axis
  359. if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, C_AXIS))) {
  360. if (TEST(out_bits, Z_HEAD))
  361. #else
  362. if (TEST(out_bits, Z_AXIS))
  363. #endif
  364. { // z -direction
  365. #if HAS_Z_MIN
  366. #if ENABLED(Z_DUAL_ENDSTOPS)
  367. SET_ENDSTOP_BIT(Z, MIN);
  368. #if HAS_Z2_MIN
  369. SET_ENDSTOP_BIT(Z2, MIN);
  370. #else
  371. COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
  372. #endif
  373. byte z_test = TEST_ENDSTOP(Z_MIN) | (TEST_ENDSTOP(Z2_MIN) << 1); // bit 0 for Z, bit 1 for Z2
  374. if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
  375. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  376. SBI(endstop_hit_bits, Z_MIN);
  377. if (!performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  378. step_events_completed = current_block->step_event_count;
  379. }
  380. #else // !Z_DUAL_ENDSTOPS
  381. UPDATE_ENDSTOP(Z, MIN);
  382. #endif // !Z_DUAL_ENDSTOPS
  383. #endif // Z_MIN_PIN
  384. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  385. UPDATE_ENDSTOP(Z, MIN_PROBE);
  386. if (TEST_ENDSTOP(Z_MIN_PROBE)) {
  387. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  388. SBI(endstop_hit_bits, Z_MIN_PROBE);
  389. }
  390. #endif
  391. }
  392. else { // z +direction
  393. #if HAS_Z_MAX
  394. #if ENABLED(Z_DUAL_ENDSTOPS)
  395. SET_ENDSTOP_BIT(Z, MAX);
  396. #if HAS_Z2_MAX
  397. SET_ENDSTOP_BIT(Z2, MAX);
  398. #else
  399. COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
  400. #endif
  401. byte z_test = TEST_ENDSTOP(Z_MAX) | (TEST_ENDSTOP(Z2_MAX) << 1); // bit 0 for Z, bit 1 for Z2
  402. if (z_test && current_block->steps[Z_AXIS] > 0) { // t_test = Z_MAX || Z2_MAX
  403. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  404. SBI(endstop_hit_bits, Z_MIN);
  405. if (!performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  406. step_events_completed = current_block->step_event_count;
  407. }
  408. #else // !Z_DUAL_ENDSTOPS
  409. UPDATE_ENDSTOP(Z, MAX);
  410. #endif // !Z_DUAL_ENDSTOPS
  411. #endif // Z_MAX_PIN
  412. }
  413. #if ENABLED(COREXZ)
  414. }
  415. #endif
  416. old_endstop_bits = current_endstop_bits;
  417. }
  418. // __________________________
  419. // /| |\ _________________ ^
  420. // / | | \ /| |\ |
  421. // / | | \ / | | \ s
  422. // / | | | | | \ p
  423. // / | | | | | \ e
  424. // +-----+------------------------+---+--+---------------+----+ e
  425. // | BLOCK 1 | BLOCK 2 | d
  426. //
  427. // time ----->
  428. //
  429. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  430. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  431. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  432. // The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  433. void st_wake_up() {
  434. // TCNT1 = 0;
  435. ENABLE_STEPPER_DRIVER_INTERRUPT();
  436. }
  437. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  438. unsigned short timer;
  439. NOMORE(step_rate, MAX_STEP_FREQUENCY);
  440. if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  441. step_rate = (step_rate >> 2) & 0x3fff;
  442. step_loops = 4;
  443. }
  444. else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  445. step_rate = (step_rate >> 1) & 0x7fff;
  446. step_loops = 2;
  447. }
  448. else {
  449. step_loops = 1;
  450. }
  451. NOLESS(step_rate, F_CPU / 500000);
  452. step_rate -= F_CPU / 500000; // Correct for minimal speed
  453. if (step_rate >= (8 * 256)) { // higher step rate
  454. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
  455. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  456. unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
  457. MultiU16X8toH16(timer, tmp_step_rate, gain);
  458. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  459. }
  460. else { // lower step rates
  461. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  462. table_address += ((step_rate) >> 1) & 0xfffc;
  463. timer = (unsigned short)pgm_read_word_near(table_address);
  464. timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
  465. }
  466. if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  467. return timer;
  468. }
  469. /**
  470. * Set the stepper direction of each axis
  471. *
  472. * X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY
  473. * X_AXIS=A_AXIS and Z_AXIS=C_AXIS for COREXZ
  474. */
  475. void set_stepper_direction() {
  476. #define SET_STEP_DIR(AXIS) \
  477. if (TEST(out_bits, AXIS ##_AXIS)) { \
  478. AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
  479. count_direction[AXIS ##_AXIS] = -1; \
  480. } \
  481. else { \
  482. AXIS ##_APPLY_DIR(!INVERT_## AXIS ##_DIR, false); \
  483. count_direction[AXIS ##_AXIS] = 1; \
  484. }
  485. SET_STEP_DIR(X); // A
  486. SET_STEP_DIR(Y); // B
  487. SET_STEP_DIR(Z); // C
  488. #if DISABLED(ADVANCE)
  489. if (TEST(out_bits, E_AXIS)) {
  490. REV_E_DIR();
  491. count_direction[E_AXIS] = -1;
  492. }
  493. else {
  494. NORM_E_DIR();
  495. count_direction[E_AXIS] = 1;
  496. }
  497. #endif //!ADVANCE
  498. }
  499. // Initializes the trapezoid generator from the current block. Called whenever a new
  500. // block begins.
  501. FORCE_INLINE void trapezoid_generator_reset() {
  502. static int8_t last_extruder = -1;
  503. if (current_block->direction_bits != out_bits || current_block->active_extruder != last_extruder) {
  504. out_bits = current_block->direction_bits;
  505. last_extruder = current_block->active_extruder;
  506. set_stepper_direction();
  507. }
  508. #if ENABLED(ADVANCE)
  509. advance = current_block->initial_advance;
  510. final_advance = current_block->final_advance;
  511. // Do E steps + advance steps
  512. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  513. old_advance = advance >>8;
  514. #endif
  515. deceleration_time = 0;
  516. // step_rate to timer interval
  517. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  518. // make a note of the number of step loops required at nominal speed
  519. step_loops_nominal = step_loops;
  520. acc_step_rate = current_block->initial_rate;
  521. acceleration_time = calc_timer(acc_step_rate);
  522. OCR1A = acceleration_time;
  523. // SERIAL_ECHO_START;
  524. // SERIAL_ECHOPGM("advance :");
  525. // SERIAL_ECHO(current_block->advance/256.0);
  526. // SERIAL_ECHOPGM("advance rate :");
  527. // SERIAL_ECHO(current_block->advance_rate/256.0);
  528. // SERIAL_ECHOPGM("initial advance :");
  529. // SERIAL_ECHO(current_block->initial_advance/256.0);
  530. // SERIAL_ECHOPGM("final advance :");
  531. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  532. }
  533. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  534. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  535. ISR(TIMER1_COMPA_vect) {
  536. if (cleaning_buffer_counter) {
  537. current_block = NULL;
  538. plan_discard_current_block();
  539. #ifdef SD_FINISHED_RELEASECOMMAND
  540. if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  541. #endif
  542. cleaning_buffer_counter--;
  543. OCR1A = 200;
  544. return;
  545. }
  546. // If there is no current block, attempt to pop one from the buffer
  547. if (!current_block) {
  548. // Anything in the buffer?
  549. current_block = plan_get_current_block();
  550. if (current_block) {
  551. current_block->busy = true;
  552. trapezoid_generator_reset();
  553. counter_x = -(current_block->step_event_count >> 1);
  554. counter_y = counter_z = counter_e = counter_x;
  555. step_events_completed = 0;
  556. #if ENABLED(Z_LATE_ENABLE)
  557. if (current_block->steps[Z_AXIS] > 0) {
  558. enable_z();
  559. OCR1A = 2000; //1ms wait
  560. return;
  561. }
  562. #endif
  563. // #if ENABLED(ADVANCE)
  564. // e_steps[current_block->active_extruder] = 0;
  565. // #endif
  566. }
  567. else {
  568. OCR1A = 2000; // 1kHz.
  569. }
  570. }
  571. if (current_block != NULL) {
  572. // Update endstops state, if enabled
  573. if (check_endstops) update_endstops();
  574. // Take multiple steps per interrupt (For high speed moves)
  575. for (int8_t i = 0; i < step_loops; i++) {
  576. #ifndef USBCON
  577. customizedSerial.checkRx(); // Check for serial chars.
  578. #endif
  579. #if ENABLED(ADVANCE)
  580. counter_e += current_block->steps[E_AXIS];
  581. if (counter_e > 0) {
  582. counter_e -= current_block->step_event_count;
  583. e_steps[current_block->active_extruder] += TEST(out_bits, E_AXIS) ? -1 : 1;
  584. }
  585. #endif //ADVANCE
  586. #define _COUNTER(axis) counter_## axis
  587. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  588. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  589. #define STEP_ADD(axis, AXIS) \
  590. _COUNTER(axis) += current_block->steps[_AXIS(AXIS)]; \
  591. if (_COUNTER(axis) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
  592. STEP_ADD(x,X);
  593. STEP_ADD(y,Y);
  594. STEP_ADD(z,Z);
  595. #if DISABLED(ADVANCE)
  596. STEP_ADD(e,E);
  597. #endif
  598. #define STEP_IF_COUNTER(axis, AXIS) \
  599. if (_COUNTER(axis) > 0) { \
  600. _COUNTER(axis) -= current_block->step_event_count; \
  601. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  602. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
  603. }
  604. STEP_IF_COUNTER(x, X);
  605. STEP_IF_COUNTER(y, Y);
  606. STEP_IF_COUNTER(z, Z);
  607. #if DISABLED(ADVANCE)
  608. STEP_IF_COUNTER(e, E);
  609. #endif
  610. step_events_completed++;
  611. if (step_events_completed >= current_block->step_event_count) break;
  612. }
  613. // Calculate new timer value
  614. unsigned short timer;
  615. unsigned short step_rate;
  616. if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
  617. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  618. acc_step_rate += current_block->initial_rate;
  619. // upper limit
  620. NOMORE(acc_step_rate, current_block->nominal_rate);
  621. // step_rate to timer interval
  622. timer = calc_timer(acc_step_rate);
  623. OCR1A = timer;
  624. acceleration_time += timer;
  625. #if ENABLED(ADVANCE)
  626. advance += advance_rate * step_loops;
  627. //NOLESS(advance, current_block->advance);
  628. // Do E steps + advance steps
  629. e_steps[current_block->active_extruder] += ((advance >> 8) - old_advance);
  630. old_advance = advance >> 8;
  631. #endif //ADVANCE
  632. }
  633. else if (step_events_completed > (unsigned long)current_block->decelerate_after) {
  634. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  635. if (step_rate <= acc_step_rate) { // Still decelerating?
  636. step_rate = acc_step_rate - step_rate;
  637. NOLESS(step_rate, current_block->final_rate);
  638. }
  639. else
  640. step_rate = current_block->final_rate;
  641. // step_rate to timer interval
  642. timer = calc_timer(step_rate);
  643. OCR1A = timer;
  644. deceleration_time += timer;
  645. #if ENABLED(ADVANCE)
  646. advance -= advance_rate * step_loops;
  647. NOLESS(advance, final_advance);
  648. // Do E steps + advance steps
  649. uint32_t advance_whole = advance >> 8;
  650. e_steps[current_block->active_extruder] += advance_whole - old_advance;
  651. old_advance = advance_whole;
  652. #endif //ADVANCE
  653. }
  654. else {
  655. OCR1A = OCR1A_nominal;
  656. // ensure we're running at the correct step rate, even if we just came off an acceleration
  657. step_loops = step_loops_nominal;
  658. }
  659. OCR1A = (OCR1A < (TCNT1 + 16)) ? (TCNT1 + 16) : OCR1A;
  660. // If current block is finished, reset pointer
  661. if (step_events_completed >= current_block->step_event_count) {
  662. current_block = NULL;
  663. plan_discard_current_block();
  664. }
  665. }
  666. }
  667. #if ENABLED(ADVANCE)
  668. unsigned char old_OCR0A;
  669. // Timer interrupt for E. e_steps is set in the main routine;
  670. // Timer 0 is shared with millies
  671. ISR(TIMER0_COMPA_vect) {
  672. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  673. OCR0A = old_OCR0A;
  674. #define STEP_E_ONCE(INDEX) \
  675. if (e_steps[INDEX] != 0) { \
  676. E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN); \
  677. if (e_steps[INDEX] < 0) { \
  678. E## INDEX ##_DIR_WRITE(INVERT_E## INDEX ##_DIR); \
  679. e_steps[INDEX]++; \
  680. } \
  681. else if (e_steps[INDEX] > 0) { \
  682. E## INDEX ##_DIR_WRITE(!INVERT_E## INDEX ##_DIR); \
  683. e_steps[INDEX]--; \
  684. } \
  685. E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); \
  686. }
  687. // Step all E steppers that have steps, up to 4 steps per interrupt
  688. for (unsigned char i = 0; i < 4; i++) {
  689. STEP_E_ONCE(0);
  690. #if EXTRUDERS > 1
  691. STEP_E_ONCE(1);
  692. #if EXTRUDERS > 2
  693. STEP_E_ONCE(2);
  694. #if EXTRUDERS > 3
  695. STEP_E_ONCE(3);
  696. #endif
  697. #endif
  698. #endif
  699. }
  700. }
  701. #endif // ADVANCE
  702. void st_init() {
  703. digipot_init(); //Initialize Digipot Motor Current
  704. microstep_init(); //Initialize Microstepping Pins
  705. // initialise TMC Steppers
  706. #if ENABLED(HAVE_TMCDRIVER)
  707. tmc_init();
  708. #endif
  709. // initialise L6470 Steppers
  710. #if ENABLED(HAVE_L6470DRIVER)
  711. L6470_init();
  712. #endif
  713. // Initialize Dir Pins
  714. #if HAS_X_DIR
  715. X_DIR_INIT;
  716. #endif
  717. #if HAS_X2_DIR
  718. X2_DIR_INIT;
  719. #endif
  720. #if HAS_Y_DIR
  721. Y_DIR_INIT;
  722. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  723. Y2_DIR_INIT;
  724. #endif
  725. #endif
  726. #if HAS_Z_DIR
  727. Z_DIR_INIT;
  728. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  729. Z2_DIR_INIT;
  730. #endif
  731. #endif
  732. #if HAS_E0_DIR
  733. E0_DIR_INIT;
  734. #endif
  735. #if HAS_E1_DIR
  736. E1_DIR_INIT;
  737. #endif
  738. #if HAS_E2_DIR
  739. E2_DIR_INIT;
  740. #endif
  741. #if HAS_E3_DIR
  742. E3_DIR_INIT;
  743. #endif
  744. //Initialize Enable Pins - steppers default to disabled.
  745. #if HAS_X_ENABLE
  746. X_ENABLE_INIT;
  747. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  748. #endif
  749. #if HAS_X2_ENABLE
  750. X2_ENABLE_INIT;
  751. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  752. #endif
  753. #if HAS_Y_ENABLE
  754. Y_ENABLE_INIT;
  755. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  756. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  757. Y2_ENABLE_INIT;
  758. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  759. #endif
  760. #endif
  761. #if HAS_Z_ENABLE
  762. Z_ENABLE_INIT;
  763. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  764. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  765. Z2_ENABLE_INIT;
  766. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  767. #endif
  768. #endif
  769. #if HAS_E0_ENABLE
  770. E0_ENABLE_INIT;
  771. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  772. #endif
  773. #if HAS_E1_ENABLE
  774. E1_ENABLE_INIT;
  775. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  776. #endif
  777. #if HAS_E2_ENABLE
  778. E2_ENABLE_INIT;
  779. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  780. #endif
  781. #if HAS_E3_ENABLE
  782. E3_ENABLE_INIT;
  783. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  784. #endif
  785. //endstops and pullups
  786. #if HAS_X_MIN
  787. SET_INPUT(X_MIN_PIN);
  788. #if ENABLED(ENDSTOPPULLUP_XMIN)
  789. WRITE(X_MIN_PIN,HIGH);
  790. #endif
  791. #endif
  792. #if HAS_Y_MIN
  793. SET_INPUT(Y_MIN_PIN);
  794. #if ENABLED(ENDSTOPPULLUP_YMIN)
  795. WRITE(Y_MIN_PIN,HIGH);
  796. #endif
  797. #endif
  798. #if HAS_Z_MIN
  799. SET_INPUT(Z_MIN_PIN);
  800. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  801. WRITE(Z_MIN_PIN,HIGH);
  802. #endif
  803. #endif
  804. #if HAS_Z2_MIN
  805. SET_INPUT(Z2_MIN_PIN);
  806. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  807. WRITE(Z2_MIN_PIN,HIGH);
  808. #endif
  809. #endif
  810. #if HAS_X_MAX
  811. SET_INPUT(X_MAX_PIN);
  812. #if ENABLED(ENDSTOPPULLUP_XMAX)
  813. WRITE(X_MAX_PIN,HIGH);
  814. #endif
  815. #endif
  816. #if HAS_Y_MAX
  817. SET_INPUT(Y_MAX_PIN);
  818. #if ENABLED(ENDSTOPPULLUP_YMAX)
  819. WRITE(Y_MAX_PIN,HIGH);
  820. #endif
  821. #endif
  822. #if HAS_Z_MAX
  823. SET_INPUT(Z_MAX_PIN);
  824. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  825. WRITE(Z_MAX_PIN,HIGH);
  826. #endif
  827. #endif
  828. #if HAS_Z2_MAX
  829. SET_INPUT(Z2_MAX_PIN);
  830. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  831. WRITE(Z2_MAX_PIN,HIGH);
  832. #endif
  833. #endif
  834. #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
  835. SET_INPUT(Z_MIN_PROBE_PIN);
  836. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  837. WRITE(Z_MIN_PROBE_PIN,HIGH);
  838. #endif
  839. #endif
  840. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  841. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  842. #define _DISABLE(axis) disable_## axis()
  843. #define AXIS_INIT(axis, AXIS, PIN) \
  844. _STEP_INIT(AXIS); \
  845. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  846. _DISABLE(axis)
  847. #define E_AXIS_INIT(NUM) AXIS_INIT(e## NUM, E## NUM, E)
  848. // Initialize Step Pins
  849. #if HAS_X_STEP
  850. AXIS_INIT(x, X, X);
  851. #endif
  852. #if HAS_X2_STEP
  853. AXIS_INIT(x, X2, X);
  854. #endif
  855. #if HAS_Y_STEP
  856. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_STEP
  857. Y2_STEP_INIT;
  858. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  859. #endif
  860. AXIS_INIT(y, Y, Y);
  861. #endif
  862. #if HAS_Z_STEP
  863. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_STEP
  864. Z2_STEP_INIT;
  865. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  866. #endif
  867. AXIS_INIT(z, Z, Z);
  868. #endif
  869. #if HAS_E0_STEP
  870. E_AXIS_INIT(0);
  871. #endif
  872. #if HAS_E1_STEP
  873. E_AXIS_INIT(1);
  874. #endif
  875. #if HAS_E2_STEP
  876. E_AXIS_INIT(2);
  877. #endif
  878. #if HAS_E3_STEP
  879. E_AXIS_INIT(3);
  880. #endif
  881. // waveform generation = 0100 = CTC
  882. CBI(TCCR1B, WGM13);
  883. SBI(TCCR1B, WGM12);
  884. CBI(TCCR1A, WGM11);
  885. CBI(TCCR1A, WGM10);
  886. // output mode = 00 (disconnected)
  887. TCCR1A &= ~(3 << COM1A0);
  888. TCCR1A &= ~(3 << COM1B0);
  889. // Set the timer pre-scaler
  890. // Generally we use a divider of 8, resulting in a 2MHz timer
  891. // frequency on a 16MHz MCU. If you are going to change this, be
  892. // sure to regenerate speed_lookuptable.h with
  893. // create_speed_lookuptable.py
  894. TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
  895. OCR1A = 0x4000;
  896. TCNT1 = 0;
  897. ENABLE_STEPPER_DRIVER_INTERRUPT();
  898. #if ENABLED(ADVANCE)
  899. #if defined(TCCR0A) && defined(WGM01)
  900. CBI(TCCR0A, WGM01);
  901. CBI(TCCR0A, WGM00);
  902. #endif
  903. e_steps[0] = e_steps[1] = e_steps[2] = e_steps[3] = 0;
  904. SBI(TIMSK0, OCIE0A);
  905. #endif //ADVANCE
  906. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  907. sei();
  908. set_stepper_direction(); // Init directions to out_bits = 0
  909. }
  910. /**
  911. * Block until all buffered steps are executed
  912. */
  913. void st_synchronize() { while (blocks_queued()) idle(); }
  914. void st_set_position(const long& x, const long& y, const long& z, const long& e) {
  915. CRITICAL_SECTION_START;
  916. count_position[X_AXIS] = x;
  917. count_position[Y_AXIS] = y;
  918. count_position[Z_AXIS] = z;
  919. count_position[E_AXIS] = e;
  920. CRITICAL_SECTION_END;
  921. }
  922. void st_set_e_position(const long& e) {
  923. CRITICAL_SECTION_START;
  924. count_position[E_AXIS] = e;
  925. CRITICAL_SECTION_END;
  926. }
  927. long st_get_position(uint8_t axis) {
  928. CRITICAL_SECTION_START;
  929. long count_pos = count_position[axis];
  930. CRITICAL_SECTION_END;
  931. return count_pos;
  932. }
  933. float st_get_axis_position_mm(AxisEnum axis) {
  934. float axis_pos;
  935. #if ENABLED(COREXY) | ENABLED(COREXZ)
  936. if (axis == X_AXIS || axis == CORE_AXIS_2) {
  937. CRITICAL_SECTION_START;
  938. long pos1 = count_position[A_AXIS],
  939. pos2 = count_position[CORE_AXIS_2];
  940. CRITICAL_SECTION_END;
  941. // ((a1+a2)+(a1-a2))/2 -> (a1+a2+a1-a2)/2 -> (a1+a1)/2 -> a1
  942. // ((a1+a2)-(a1-a2))/2 -> (a1+a2-a1+a2)/2 -> (a2+a2)/2 -> a2
  943. axis_pos = (pos1 + ((axis == X_AXIS) ? pos2 : -pos2)) / 2.0f;
  944. }
  945. else
  946. axis_pos = st_get_position(axis);
  947. #else
  948. axis_pos = st_get_position(axis);
  949. #endif
  950. return axis_pos / axis_steps_per_unit[axis];
  951. }
  952. void finishAndDisableSteppers() {
  953. st_synchronize();
  954. disable_all_steppers();
  955. }
  956. void quickStop() {
  957. cleaning_buffer_counter = 5000;
  958. DISABLE_STEPPER_DRIVER_INTERRUPT();
  959. while (blocks_queued()) plan_discard_current_block();
  960. current_block = NULL;
  961. ENABLE_STEPPER_DRIVER_INTERRUPT();
  962. }
  963. #if ENABLED(BABYSTEPPING)
  964. // MUST ONLY BE CALLED BY AN ISR,
  965. // No other ISR should ever interrupt this!
  966. void babystep(const uint8_t axis, const bool direction) {
  967. #define _ENABLE(axis) enable_## axis()
  968. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  969. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  970. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  971. #define BABYSTEP_AXIS(axis, AXIS, INVERT) { \
  972. _ENABLE(axis); \
  973. uint8_t old_pin = _READ_DIR(AXIS); \
  974. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^direction^INVERT); \
  975. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  976. delayMicroseconds(2); \
  977. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  978. _APPLY_DIR(AXIS, old_pin); \
  979. }
  980. switch (axis) {
  981. case X_AXIS:
  982. BABYSTEP_AXIS(x, X, false);
  983. break;
  984. case Y_AXIS:
  985. BABYSTEP_AXIS(y, Y, false);
  986. break;
  987. case Z_AXIS: {
  988. #if DISABLED(DELTA)
  989. BABYSTEP_AXIS(z, Z, BABYSTEP_INVERT_Z);
  990. #else // DELTA
  991. bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  992. enable_x();
  993. enable_y();
  994. enable_z();
  995. uint8_t old_x_dir_pin = X_DIR_READ,
  996. old_y_dir_pin = Y_DIR_READ,
  997. old_z_dir_pin = Z_DIR_READ;
  998. //setup new step
  999. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  1000. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  1001. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  1002. //perform step
  1003. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  1004. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1005. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1006. delayMicroseconds(2);
  1007. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1008. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1009. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  1010. //get old pin state back.
  1011. X_DIR_WRITE(old_x_dir_pin);
  1012. Y_DIR_WRITE(old_y_dir_pin);
  1013. Z_DIR_WRITE(old_z_dir_pin);
  1014. #endif
  1015. } break;
  1016. default: break;
  1017. }
  1018. }
  1019. #endif //BABYSTEPPING
  1020. // From Arduino DigitalPotControl example
  1021. void digitalPotWrite(int address, int value) {
  1022. #if HAS_DIGIPOTSS
  1023. digitalWrite(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
  1024. SPI.transfer(address); // send in the address and value via SPI:
  1025. SPI.transfer(value);
  1026. digitalWrite(DIGIPOTSS_PIN, HIGH); // take the SS pin high to de-select the chip:
  1027. //delay(10);
  1028. #else
  1029. UNUSED(address);
  1030. UNUSED(value);
  1031. #endif
  1032. }
  1033. // Initialize Digipot Motor Current
  1034. void digipot_init() {
  1035. #if HAS_DIGIPOTSS
  1036. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1037. SPI.begin();
  1038. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1039. for (int i = 0; i < COUNT(digipot_motor_current); i++) {
  1040. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1041. digipot_current(i, digipot_motor_current[i]);
  1042. }
  1043. #endif
  1044. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1045. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1046. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1047. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1048. digipot_current(0, motor_current_setting[0]);
  1049. digipot_current(1, motor_current_setting[1]);
  1050. digipot_current(2, motor_current_setting[2]);
  1051. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1052. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1053. #endif
  1054. }
  1055. void digipot_current(uint8_t driver, int current) {
  1056. #if HAS_DIGIPOTSS
  1057. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1058. digitalPotWrite(digipot_ch[driver], current);
  1059. #elif defined(MOTOR_CURRENT_PWM_XY_PIN)
  1060. switch (driver) {
  1061. case 0: analogWrite(MOTOR_CURRENT_PWM_XY_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)); break;
  1062. case 1: analogWrite(MOTOR_CURRENT_PWM_Z_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)); break;
  1063. case 2: analogWrite(MOTOR_CURRENT_PWM_E_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)); break;
  1064. }
  1065. #else
  1066. UNUSED(driver);
  1067. UNUSED(current);
  1068. #endif
  1069. }
  1070. #if ENABLED(DAC_STEPPER_CURRENT)
  1071. bool dac_present = false;
  1072. const uint8_t dac_order[NUM_AXIS] = DAC_STEPPER_ORDER;
  1073. int dac_init() {
  1074. mcp4728_init();
  1075. if (mcp4728_simpleCommand(RESET)) return -1;
  1076. dac_present = true;
  1077. mcp4728_setVref_all(DAC_STEPPER_VREF);
  1078. mcp4728_setGain_all(DAC_STEPPER_GAIN);
  1079. return 0;
  1080. }
  1081. void dac_current_percent(uint8_t channel, float val) {
  1082. if (!dac_present) return;
  1083. NOMORE(val, 100);
  1084. mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100);
  1085. mcp4728_simpleCommand(UPDATE);
  1086. }
  1087. void dac_current_raw(uint8_t channel, uint16_t val) {
  1088. if (!dac_present) return;
  1089. NOMORE(val, DAC_STEPPER_MAX);
  1090. mcp4728_analogWrite(dac_order[channel], val);
  1091. mcp4728_simpleCommand(UPDATE);
  1092. }
  1093. static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; }
  1094. static float dac_amps(int8_t n) { return ((2.048 * mcp4728_getValue(dac_order[n])) / 4096.0) / (8.0 * DAC_STEPPER_SENSE); }
  1095. void dac_print_values() {
  1096. if (!dac_present) return;
  1097. SERIAL_ECHO_START;
  1098. SERIAL_ECHOLNPGM("Stepper current values in % (Amps):");
  1099. SERIAL_ECHO_START;
  1100. SERIAL_ECHOPAIR(" X:", dac_perc(0));
  1101. SERIAL_ECHOPAIR(" (", dac_amps(0));
  1102. SERIAL_ECHOPAIR(") Y:", dac_perc(1));
  1103. SERIAL_ECHOPAIR(" (", dac_amps(1));
  1104. SERIAL_ECHOPAIR(") Z:", dac_perc(2));
  1105. SERIAL_ECHOPAIR(" (", dac_amps(2));
  1106. SERIAL_ECHOPAIR(") E:", dac_perc(3));
  1107. SERIAL_ECHOPAIR(" (", dac_amps(3));
  1108. SERIAL_ECHOLN(")");
  1109. }
  1110. void dac_commit_eeprom() {
  1111. if (!dac_present) return;
  1112. mcp4728_eepromWrite();
  1113. }
  1114. #endif // DAC_STEPPER_CURRENT
  1115. void microstep_init() {
  1116. #if HAS_MICROSTEPS_E1
  1117. pinMode(E1_MS1_PIN, OUTPUT);
  1118. pinMode(E1_MS2_PIN, OUTPUT);
  1119. #endif
  1120. #if HAS_MICROSTEPS
  1121. pinMode(X_MS1_PIN, OUTPUT);
  1122. pinMode(X_MS2_PIN, OUTPUT);
  1123. pinMode(Y_MS1_PIN, OUTPUT);
  1124. pinMode(Y_MS2_PIN, OUTPUT);
  1125. pinMode(Z_MS1_PIN, OUTPUT);
  1126. pinMode(Z_MS2_PIN, OUTPUT);
  1127. pinMode(E0_MS1_PIN, OUTPUT);
  1128. pinMode(E0_MS2_PIN, OUTPUT);
  1129. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1130. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  1131. microstep_mode(i, microstep_modes[i]);
  1132. #endif
  1133. }
  1134. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
  1135. if (ms1 >= 0) switch (driver) {
  1136. case 0: digitalWrite(X_MS1_PIN, ms1); break;
  1137. case 1: digitalWrite(Y_MS1_PIN, ms1); break;
  1138. case 2: digitalWrite(Z_MS1_PIN, ms1); break;
  1139. case 3: digitalWrite(E0_MS1_PIN, ms1); break;
  1140. #if HAS_MICROSTEPS_E1
  1141. case 4: digitalWrite(E1_MS1_PIN, ms1); break;
  1142. #endif
  1143. }
  1144. if (ms2 >= 0) switch (driver) {
  1145. case 0: digitalWrite(X_MS2_PIN, ms2); break;
  1146. case 1: digitalWrite(Y_MS2_PIN, ms2); break;
  1147. case 2: digitalWrite(Z_MS2_PIN, ms2); break;
  1148. case 3: digitalWrite(E0_MS2_PIN, ms2); break;
  1149. #if PIN_EXISTS(E1_MS2)
  1150. case 4: digitalWrite(E1_MS2_PIN, ms2); break;
  1151. #endif
  1152. }
  1153. }
  1154. void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
  1155. switch (stepping_mode) {
  1156. case 1: microstep_ms(driver, MICROSTEP1); break;
  1157. case 2: microstep_ms(driver, MICROSTEP2); break;
  1158. case 4: microstep_ms(driver, MICROSTEP4); break;
  1159. case 8: microstep_ms(driver, MICROSTEP8); break;
  1160. case 16: microstep_ms(driver, MICROSTEP16); break;
  1161. }
  1162. }
  1163. void microstep_readings() {
  1164. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1165. SERIAL_PROTOCOLPGM("X: ");
  1166. SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
  1167. SERIAL_PROTOCOLLN(digitalRead(X_MS2_PIN));
  1168. SERIAL_PROTOCOLPGM("Y: ");
  1169. SERIAL_PROTOCOL(digitalRead(Y_MS1_PIN));
  1170. SERIAL_PROTOCOLLN(digitalRead(Y_MS2_PIN));
  1171. SERIAL_PROTOCOLPGM("Z: ");
  1172. SERIAL_PROTOCOL(digitalRead(Z_MS1_PIN));
  1173. SERIAL_PROTOCOLLN(digitalRead(Z_MS2_PIN));
  1174. SERIAL_PROTOCOLPGM("E0: ");
  1175. SERIAL_PROTOCOL(digitalRead(E0_MS1_PIN));
  1176. SERIAL_PROTOCOLLN(digitalRead(E0_MS2_PIN));
  1177. #if HAS_MICROSTEPS_E1
  1178. SERIAL_PROTOCOLPGM("E1: ");
  1179. SERIAL_PROTOCOL(digitalRead(E1_MS1_PIN));
  1180. SERIAL_PROTOCOLLN(digitalRead(E1_MS2_PIN));
  1181. #endif
  1182. }
  1183. #if ENABLED(Z_DUAL_ENDSTOPS)
  1184. void In_Homing_Process(bool state) { performing_homing = state; }
  1185. void Lock_z_motor(bool state) { locked_z_motor = state; }
  1186. void Lock_z2_motor(bool state) { locked_z2_motor = state; }
  1187. #endif