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

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