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.

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