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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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 deccelaration 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 bool endstop_x_hit = false;
  62. static volatile bool endstop_y_hit = false;
  63. static volatile bool endstop_z_hit = false;
  64. static volatile bool endstop_z_probe_hit = false; // Leaving this in even if Z_PROBE_ENDSTOP isn't defined, keeps code below cleaner. #ifdef it and usage below to save space.
  65. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  66. bool abort_on_endstop_hit = false;
  67. #endif
  68. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  69. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  70. #endif
  71. #if HAS_X_MIN
  72. static bool old_x_min_endstop = false;
  73. #endif
  74. #if HAS_X_MAX
  75. static bool old_x_max_endstop = false;
  76. #endif
  77. #if HAS_Y_MIN
  78. static bool old_y_min_endstop = false;
  79. #endif
  80. #if HAS_Y_MAX
  81. static bool old_y_max_endstop = false;
  82. #endif
  83. static bool old_z_min_endstop = false;
  84. static bool old_z_max_endstop = false;
  85. #ifdef Z_DUAL_ENDSTOPS
  86. static bool old_z2_min_endstop = false;
  87. static bool old_z2_max_endstop = false;
  88. #endif
  89. #ifdef Z_PROBE_ENDSTOP // No need to check for valid pin, SanityCheck.h already does this.
  90. static bool old_z_probe_endstop = false;
  91. #endif
  92. static bool check_endstops = true;
  93. volatile long count_position[NUM_AXIS] = { 0 };
  94. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
  95. //===========================================================================
  96. //================================ functions ================================
  97. //===========================================================================
  98. #ifdef DUAL_X_CARRIAGE
  99. #define X_APPLY_DIR(v,ALWAYS) \
  100. if (extruder_duplication_enabled || ALWAYS) { \
  101. X_DIR_WRITE(v); \
  102. X2_DIR_WRITE(v); \
  103. } \
  104. else { \
  105. if (current_block->active_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  106. }
  107. #define X_APPLY_STEP(v,ALWAYS) \
  108. if (extruder_duplication_enabled || ALWAYS) { \
  109. X_STEP_WRITE(v); \
  110. X2_STEP_WRITE(v); \
  111. } \
  112. else { \
  113. if (current_block->active_extruder != 0) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  114. }
  115. #else
  116. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  117. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  118. #endif
  119. #ifdef Y_DUAL_STEPPER_DRIVERS
  120. #define Y_APPLY_DIR(v,Q) { Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }
  121. #define Y_APPLY_STEP(v,Q) { Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }
  122. #else
  123. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  124. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  125. #endif
  126. #ifdef Z_DUAL_STEPPER_DRIVERS
  127. #define Z_APPLY_DIR(v,Q) { Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }
  128. #ifdef Z_DUAL_ENDSTOPS
  129. #define Z_APPLY_STEP(v,Q) \
  130. if (performing_homing) { \
  131. if (Z_HOME_DIR > 0) {\
  132. if (!(old_z_max_endstop && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  133. if (!(old_z2_max_endstop && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  134. } else {\
  135. if (!(old_z_min_endstop && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  136. if (!(old_z2_min_endstop && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  137. } \
  138. } else { \
  139. Z_STEP_WRITE(v); \
  140. Z2_STEP_WRITE(v); \
  141. }
  142. #else
  143. #define Z_APPLY_STEP(v,Q) { Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }
  144. #endif
  145. #else
  146. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  147. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  148. #endif
  149. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
  150. // intRes = intIn1 * intIn2 >> 16
  151. // uses:
  152. // r26 to store 0
  153. // r27 to store the byte 1 of the 24 bit result
  154. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  155. asm volatile ( \
  156. "clr r26 \n\t" \
  157. "mul %A1, %B2 \n\t" \
  158. "movw %A0, r0 \n\t" \
  159. "mul %A1, %A2 \n\t" \
  160. "add %A0, r1 \n\t" \
  161. "adc %B0, r26 \n\t" \
  162. "lsr r0 \n\t" \
  163. "adc %A0, r26 \n\t" \
  164. "adc %B0, r26 \n\t" \
  165. "clr r1 \n\t" \
  166. : \
  167. "=&r" (intRes) \
  168. : \
  169. "d" (charIn1), \
  170. "d" (intIn2) \
  171. : \
  172. "r26" \
  173. )
  174. // intRes = longIn1 * longIn2 >> 24
  175. // uses:
  176. // r26 to store 0
  177. // r27 to store the byte 1 of the 48bit result
  178. // intRes = longIn1 * longIn2 >> 24
  179. // uses:
  180. // r26 to store 0
  181. // r27 to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
  182. // note that the lower two bytes and the upper byte of the 48bit result are not calculated.
  183. // this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
  184. // B0 A0 are bits 24-39 and are the returned value
  185. // C1 B1 A1 is longIn1
  186. // D2 C2 B2 A2 is longIn2
  187. //
  188. #define MultiU24X32toH16(intRes, longIn1, longIn2) \
  189. asm volatile ( \
  190. "clr r26 \n\t" \
  191. "mul %A1, %B2 \n\t" \
  192. "mov r27, r1 \n\t" \
  193. "mul %B1, %C2 \n\t" \
  194. "movw %A0, r0 \n\t" \
  195. "mul %C1, %C2 \n\t" \
  196. "add %B0, r0 \n\t" \
  197. "mul %C1, %B2 \n\t" \
  198. "add %A0, r0 \n\t" \
  199. "adc %B0, r1 \n\t" \
  200. "mul %A1, %C2 \n\t" \
  201. "add r27, r0 \n\t" \
  202. "adc %A0, r1 \n\t" \
  203. "adc %B0, r26 \n\t" \
  204. "mul %B1, %B2 \n\t" \
  205. "add r27, r0 \n\t" \
  206. "adc %A0, r1 \n\t" \
  207. "adc %B0, r26 \n\t" \
  208. "mul %C1, %A2 \n\t" \
  209. "add r27, r0 \n\t" \
  210. "adc %A0, r1 \n\t" \
  211. "adc %B0, r26 \n\t" \
  212. "mul %B1, %A2 \n\t" \
  213. "add r27, r1 \n\t" \
  214. "adc %A0, r26 \n\t" \
  215. "adc %B0, r26 \n\t" \
  216. "lsr r27 \n\t" \
  217. "adc %A0, r26 \n\t" \
  218. "adc %B0, r26 \n\t" \
  219. "mul %D2, %A1 \n\t" \
  220. "add %A0, r0 \n\t" \
  221. "adc %B0, r1 \n\t" \
  222. "mul %D2, %B1 \n\t" \
  223. "add %B0, r0 \n\t" \
  224. "clr r1 \n\t" \
  225. : \
  226. "=&r" (intRes) \
  227. : \
  228. "d" (longIn1), \
  229. "d" (longIn2) \
  230. : \
  231. "r26" , "r27" \
  232. )
  233. // Some useful constants
  234. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= BIT(OCIE1A)
  235. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~BIT(OCIE1A)
  236. void endstops_hit_on_purpose() {
  237. endstop_x_hit = endstop_y_hit = endstop_z_hit = endstop_z_probe_hit = false; // #ifdef endstop_z_probe_hit = to save space if needed.
  238. }
  239. void checkHitEndstops() {
  240. if (endstop_x_hit || endstop_y_hit || endstop_z_hit || endstop_z_probe_hit) { // #ifdef || endstop_z_probe_hit to save space if needed.
  241. SERIAL_ECHO_START;
  242. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  243. if (endstop_x_hit) {
  244. SERIAL_ECHOPAIR(" X:", (float)endstops_trigsteps[X_AXIS] / axis_steps_per_unit[X_AXIS]);
  245. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "X");
  246. }
  247. if (endstop_y_hit) {
  248. SERIAL_ECHOPAIR(" Y:", (float)endstops_trigsteps[Y_AXIS] / axis_steps_per_unit[Y_AXIS]);
  249. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Y");
  250. }
  251. if (endstop_z_hit) {
  252. SERIAL_ECHOPAIR(" Z:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
  253. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
  254. }
  255. #ifdef Z_PROBE_ENDSTOP
  256. if (endstop_z_probe_hit) {
  257. SERIAL_ECHOPAIR(" Z_PROBE:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
  258. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "ZP");
  259. }
  260. #endif
  261. SERIAL_EOL;
  262. endstops_hit_on_purpose();
  263. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  264. if (abort_on_endstop_hit) {
  265. card.sdprinting = false;
  266. card.closefile();
  267. quickStop();
  268. setTargetHotend0(0);
  269. setTargetHotend1(0);
  270. setTargetHotend2(0);
  271. setTargetHotend3(0);
  272. setTargetBed(0);
  273. }
  274. #endif
  275. }
  276. }
  277. void enable_endstops(bool check) { check_endstops = check; }
  278. // __________________________
  279. // /| |\ _________________ ^
  280. // / | | \ /| |\ |
  281. // / | | \ / | | \ s
  282. // / | | | | | \ p
  283. // / | | | | | \ e
  284. // +-----+------------------------+---+--+---------------+----+ e
  285. // | BLOCK 1 | BLOCK 2 | d
  286. //
  287. // time ----->
  288. //
  289. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  290. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  291. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  292. // The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  293. void st_wake_up() {
  294. // TCNT1 = 0;
  295. ENABLE_STEPPER_DRIVER_INTERRUPT();
  296. }
  297. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  298. unsigned short timer;
  299. if (step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  300. if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  301. step_rate = (step_rate >> 2) & 0x3fff;
  302. step_loops = 4;
  303. }
  304. else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  305. step_rate = (step_rate >> 1) & 0x7fff;
  306. step_loops = 2;
  307. }
  308. else {
  309. step_loops = 1;
  310. }
  311. if (step_rate < (F_CPU / 500000)) step_rate = (F_CPU / 500000);
  312. step_rate -= (F_CPU / 500000); // Correct for minimal speed
  313. if (step_rate >= (8 * 256)) { // higher step rate
  314. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  315. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  316. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  317. MultiU16X8toH16(timer, tmp_step_rate, gain);
  318. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  319. }
  320. else { // lower step rates
  321. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  322. table_address += ((step_rate)>>1) & 0xfffc;
  323. timer = (unsigned short)pgm_read_word_near(table_address);
  324. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  325. }
  326. if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  327. return timer;
  328. }
  329. // Initializes the trapezoid generator from the current block. Called whenever a new
  330. // block begins.
  331. FORCE_INLINE void trapezoid_generator_reset() {
  332. #ifdef ADVANCE
  333. advance = current_block->initial_advance;
  334. final_advance = current_block->final_advance;
  335. // Do E steps + advance steps
  336. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  337. old_advance = advance >>8;
  338. #endif
  339. deceleration_time = 0;
  340. // step_rate to timer interval
  341. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  342. // make a note of the number of step loops required at nominal speed
  343. step_loops_nominal = step_loops;
  344. acc_step_rate = current_block->initial_rate;
  345. acceleration_time = calc_timer(acc_step_rate);
  346. OCR1A = acceleration_time;
  347. // SERIAL_ECHO_START;
  348. // SERIAL_ECHOPGM("advance :");
  349. // SERIAL_ECHO(current_block->advance/256.0);
  350. // SERIAL_ECHOPGM("advance rate :");
  351. // SERIAL_ECHO(current_block->advance_rate/256.0);
  352. // SERIAL_ECHOPGM("initial advance :");
  353. // SERIAL_ECHO(current_block->initial_advance/256.0);
  354. // SERIAL_ECHOPGM("final advance :");
  355. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  356. }
  357. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  358. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  359. ISR(TIMER1_COMPA_vect) {
  360. if(cleaning_buffer_counter)
  361. {
  362. current_block = NULL;
  363. plan_discard_current_block();
  364. #ifdef SD_FINISHED_RELEASECOMMAND
  365. if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueuecommands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  366. #endif
  367. cleaning_buffer_counter--;
  368. OCR1A = 200;
  369. return;
  370. }
  371. // If there is no current block, attempt to pop one from the buffer
  372. if (!current_block) {
  373. // Anything in the buffer?
  374. current_block = plan_get_current_block();
  375. if (current_block) {
  376. current_block->busy = true;
  377. trapezoid_generator_reset();
  378. counter_x = -(current_block->step_event_count >> 1);
  379. counter_y = counter_z = counter_e = counter_x;
  380. step_events_completed = 0;
  381. #ifdef Z_LATE_ENABLE
  382. if (current_block->steps[Z_AXIS] > 0) {
  383. enable_z();
  384. OCR1A = 2000; //1ms wait
  385. return;
  386. }
  387. #endif
  388. // #ifdef ADVANCE
  389. // e_steps[current_block->active_extruder] = 0;
  390. // #endif
  391. }
  392. else {
  393. OCR1A = 2000; // 1kHz.
  394. }
  395. }
  396. if (current_block != NULL) {
  397. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  398. out_bits = current_block->direction_bits;
  399. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  400. if (TEST(out_bits, X_AXIS)) {
  401. X_APPLY_DIR(INVERT_X_DIR,0);
  402. count_direction[X_AXIS] = -1;
  403. }
  404. else {
  405. X_APPLY_DIR(!INVERT_X_DIR,0);
  406. count_direction[X_AXIS] = 1;
  407. }
  408. if (TEST(out_bits, Y_AXIS)) {
  409. Y_APPLY_DIR(INVERT_Y_DIR,0);
  410. count_direction[Y_AXIS] = -1;
  411. }
  412. else {
  413. Y_APPLY_DIR(!INVERT_Y_DIR,0);
  414. count_direction[Y_AXIS] = 1;
  415. }
  416. #define UPDATE_ENDSTOP(axis,AXIS,minmax,MINMAX) \
  417. bool axis ##_## minmax ##_endstop = (READ(AXIS ##_## MINMAX ##_PIN) != AXIS ##_## MINMAX ##_ENDSTOP_INVERTING); \
  418. if (axis ##_## minmax ##_endstop && old_## axis ##_## minmax ##_endstop && (current_block->steps[AXIS ##_AXIS] > 0)) { \
  419. endstops_trigsteps[AXIS ##_AXIS] = count_position[AXIS ##_AXIS]; \
  420. endstop_## axis ##_hit = true; \
  421. step_events_completed = current_block->step_event_count; \
  422. } \
  423. old_## axis ##_## minmax ##_endstop = axis ##_## minmax ##_endstop;
  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 cartesians 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_z_hit = true;
  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_z_probe_hit=true;
  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_z_hit = true;
  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_z_probe_hit=true;
  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. #ifdef CONFIG_STEPPERS_TOSHIBA
  586. /**
  587. * The Toshiba stepper controller require much longer pulses.
  588. * So we 'stage' decompose the pulses between high and low
  589. * instead of doing each in turn. The extra tests add enough
  590. * lag to allow it work with without needing NOPs
  591. */
  592. #define STEP_ADD(axis, AXIS) \
  593. counter_## axis += current_block->steps[AXIS ##_AXIS]; \
  594. if (counter_## axis > 0) { AXIS ##_STEP_WRITE(HIGH); }
  595. STEP_ADD(x,X);
  596. STEP_ADD(y,Y);
  597. STEP_ADD(z,Z);
  598. #ifndef ADVANCE
  599. STEP_ADD(e,E);
  600. #endif
  601. #define STEP_IF_COUNTER(axis, AXIS) \
  602. if (counter_## axis > 0) { \
  603. counter_## axis -= current_block->step_event_count; \
  604. count_position[AXIS ##_AXIS] += count_direction[AXIS ##_AXIS]; \
  605. AXIS ##_STEP_WRITE(LOW); \
  606. }
  607. STEP_IF_COUNTER(x, X);
  608. STEP_IF_COUNTER(y, Y);
  609. STEP_IF_COUNTER(z, Z);
  610. #ifndef ADVANCE
  611. STEP_IF_COUNTER(e, E);
  612. #endif
  613. #else // !CONFIG_STEPPERS_TOSHIBA
  614. #define APPLY_MOVEMENT(axis, AXIS) \
  615. counter_## axis += current_block->steps[AXIS ##_AXIS]; \
  616. if (counter_## axis > 0) { \
  617. AXIS ##_APPLY_STEP(!INVERT_## AXIS ##_STEP_PIN,0); \
  618. counter_## axis -= current_block->step_event_count; \
  619. count_position[AXIS ##_AXIS] += count_direction[AXIS ##_AXIS]; \
  620. AXIS ##_APPLY_STEP(INVERT_## AXIS ##_STEP_PIN,0); \
  621. }
  622. APPLY_MOVEMENT(x, X);
  623. APPLY_MOVEMENT(y, Y);
  624. APPLY_MOVEMENT(z, Z);
  625. #ifndef ADVANCE
  626. APPLY_MOVEMENT(e, E);
  627. #endif
  628. #endif // CONFIG_STEPPERS_TOSHIBA
  629. step_events_completed++;
  630. if (step_events_completed >= current_block->step_event_count) break;
  631. }
  632. // Calculate new timer value
  633. unsigned short timer;
  634. unsigned short step_rate;
  635. if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
  636. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  637. acc_step_rate += current_block->initial_rate;
  638. // upper limit
  639. if (acc_step_rate > current_block->nominal_rate)
  640. acc_step_rate = current_block->nominal_rate;
  641. // step_rate to timer interval
  642. timer = calc_timer(acc_step_rate);
  643. OCR1A = timer;
  644. acceleration_time += timer;
  645. #ifdef ADVANCE
  646. for(int8_t i=0; i < step_loops; i++) {
  647. advance += advance_rate;
  648. }
  649. //if (advance > current_block->advance) advance = current_block->advance;
  650. // Do E steps + advance steps
  651. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  652. old_advance = advance >>8;
  653. #endif
  654. }
  655. else if (step_events_completed > (unsigned long)current_block->decelerate_after) {
  656. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  657. if (step_rate > acc_step_rate) { // Check step_rate stays positive
  658. step_rate = current_block->final_rate;
  659. }
  660. else {
  661. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  662. }
  663. // lower limit
  664. if (step_rate < current_block->final_rate)
  665. step_rate = current_block->final_rate;
  666. // step_rate to timer interval
  667. timer = calc_timer(step_rate);
  668. OCR1A = timer;
  669. deceleration_time += timer;
  670. #ifdef ADVANCE
  671. for(int8_t i=0; i < step_loops; i++) {
  672. advance -= advance_rate;
  673. }
  674. if (advance < final_advance) advance = final_advance;
  675. // Do E steps + advance steps
  676. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  677. old_advance = advance >>8;
  678. #endif //ADVANCE
  679. }
  680. else {
  681. OCR1A = OCR1A_nominal;
  682. // ensure we're running at the correct step rate, even if we just came off an acceleration
  683. step_loops = step_loops_nominal;
  684. }
  685. // If current block is finished, reset pointer
  686. if (step_events_completed >= current_block->step_event_count) {
  687. current_block = NULL;
  688. plan_discard_current_block();
  689. }
  690. }
  691. }
  692. #ifdef ADVANCE
  693. unsigned char old_OCR0A;
  694. // Timer interrupt for E. e_steps is set in the main routine;
  695. // Timer 0 is shared with millies
  696. ISR(TIMER0_COMPA_vect)
  697. {
  698. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  699. OCR0A = old_OCR0A;
  700. // Set E direction (Depends on E direction + advance)
  701. for(unsigned char i=0; i<4;i++) {
  702. if (e_steps[0] != 0) {
  703. E0_STEP_WRITE(INVERT_E_STEP_PIN);
  704. if (e_steps[0] < 0) {
  705. E0_DIR_WRITE(INVERT_E0_DIR);
  706. e_steps[0]++;
  707. E0_STEP_WRITE(!INVERT_E_STEP_PIN);
  708. }
  709. else if (e_steps[0] > 0) {
  710. E0_DIR_WRITE(!INVERT_E0_DIR);
  711. e_steps[0]--;
  712. E0_STEP_WRITE(!INVERT_E_STEP_PIN);
  713. }
  714. }
  715. #if EXTRUDERS > 1
  716. if (e_steps[1] != 0) {
  717. E1_STEP_WRITE(INVERT_E_STEP_PIN);
  718. if (e_steps[1] < 0) {
  719. E1_DIR_WRITE(INVERT_E1_DIR);
  720. e_steps[1]++;
  721. E1_STEP_WRITE(!INVERT_E_STEP_PIN);
  722. }
  723. else if (e_steps[1] > 0) {
  724. E1_DIR_WRITE(!INVERT_E1_DIR);
  725. e_steps[1]--;
  726. E1_STEP_WRITE(!INVERT_E_STEP_PIN);
  727. }
  728. }
  729. #endif
  730. #if EXTRUDERS > 2
  731. if (e_steps[2] != 0) {
  732. E2_STEP_WRITE(INVERT_E_STEP_PIN);
  733. if (e_steps[2] < 0) {
  734. E2_DIR_WRITE(INVERT_E2_DIR);
  735. e_steps[2]++;
  736. E2_STEP_WRITE(!INVERT_E_STEP_PIN);
  737. }
  738. else if (e_steps[2] > 0) {
  739. E2_DIR_WRITE(!INVERT_E2_DIR);
  740. e_steps[2]--;
  741. E2_STEP_WRITE(!INVERT_E_STEP_PIN);
  742. }
  743. }
  744. #endif
  745. #if EXTRUDERS > 3
  746. if (e_steps[3] != 0) {
  747. E3_STEP_WRITE(INVERT_E_STEP_PIN);
  748. if (e_steps[3] < 0) {
  749. E3_DIR_WRITE(INVERT_E3_DIR);
  750. e_steps[3]++;
  751. E3_STEP_WRITE(!INVERT_E_STEP_PIN);
  752. }
  753. else if (e_steps[3] > 0) {
  754. E3_DIR_WRITE(!INVERT_E3_DIR);
  755. e_steps[3]--;
  756. E3_STEP_WRITE(!INVERT_E_STEP_PIN);
  757. }
  758. }
  759. #endif
  760. }
  761. }
  762. #endif // ADVANCE
  763. void st_init() {
  764. digipot_init(); //Initialize Digipot Motor Current
  765. microstep_init(); //Initialize Microstepping Pins
  766. // initialise TMC Steppers
  767. #ifdef HAVE_TMCDRIVER
  768. tmc_init();
  769. #endif
  770. // initialise L6470 Steppers
  771. #ifdef HAVE_L6470DRIVER
  772. L6470_init();
  773. #endif
  774. // Initialize Dir Pins
  775. #if HAS_X_DIR
  776. X_DIR_INIT;
  777. #endif
  778. #if HAS_X2_DIR
  779. X2_DIR_INIT;
  780. #endif
  781. #if HAS_Y_DIR
  782. Y_DIR_INIT;
  783. #if defined(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  784. Y2_DIR_INIT;
  785. #endif
  786. #endif
  787. #if HAS_Z_DIR
  788. Z_DIR_INIT;
  789. #if defined(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  790. Z2_DIR_INIT;
  791. #endif
  792. #endif
  793. #if HAS_E0_DIR
  794. E0_DIR_INIT;
  795. #endif
  796. #if HAS_E1_DIR
  797. E1_DIR_INIT;
  798. #endif
  799. #if HAS_E2_DIR
  800. E2_DIR_INIT;
  801. #endif
  802. #if HAS_E3_DIR
  803. E3_DIR_INIT;
  804. #endif
  805. //Initialize Enable Pins - steppers default to disabled.
  806. #if HAS_X_ENABLE
  807. X_ENABLE_INIT;
  808. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  809. #endif
  810. #if HAS_X2_ENABLE
  811. X2_ENABLE_INIT;
  812. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  813. #endif
  814. #if HAS_Y_ENABLE
  815. Y_ENABLE_INIT;
  816. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  817. #if defined(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  818. Y2_ENABLE_INIT;
  819. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  820. #endif
  821. #endif
  822. #if HAS_Z_ENABLE
  823. Z_ENABLE_INIT;
  824. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  825. #if defined(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  826. Z2_ENABLE_INIT;
  827. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  828. #endif
  829. #endif
  830. #if HAS_E0_ENABLE
  831. E0_ENABLE_INIT;
  832. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  833. #endif
  834. #if HAS_E1_ENABLE
  835. E1_ENABLE_INIT;
  836. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  837. #endif
  838. #if HAS_E2_ENABLE
  839. E2_ENABLE_INIT;
  840. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  841. #endif
  842. #if HAS_E3_ENABLE
  843. E3_ENABLE_INIT;
  844. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  845. #endif
  846. //endstops and pullups
  847. #if HAS_X_MIN
  848. SET_INPUT(X_MIN_PIN);
  849. #ifdef ENDSTOPPULLUP_XMIN
  850. WRITE(X_MIN_PIN,HIGH);
  851. #endif
  852. #endif
  853. #if HAS_Y_MIN
  854. SET_INPUT(Y_MIN_PIN);
  855. #ifdef ENDSTOPPULLUP_YMIN
  856. WRITE(Y_MIN_PIN,HIGH);
  857. #endif
  858. #endif
  859. #if HAS_Z_MIN
  860. SET_INPUT(Z_MIN_PIN);
  861. #ifdef ENDSTOPPULLUP_ZMIN
  862. WRITE(Z_MIN_PIN,HIGH);
  863. #endif
  864. #endif
  865. #if HAS_X_MAX
  866. SET_INPUT(X_MAX_PIN);
  867. #ifdef ENDSTOPPULLUP_XMAX
  868. WRITE(X_MAX_PIN,HIGH);
  869. #endif
  870. #endif
  871. #if HAS_Y_MAX
  872. SET_INPUT(Y_MAX_PIN);
  873. #ifdef ENDSTOPPULLUP_YMAX
  874. WRITE(Y_MAX_PIN,HIGH);
  875. #endif
  876. #endif
  877. #if HAS_Z_MAX
  878. SET_INPUT(Z_MAX_PIN);
  879. #ifdef ENDSTOPPULLUP_ZMAX
  880. WRITE(Z_MAX_PIN,HIGH);
  881. #endif
  882. #endif
  883. #if HAS_Z2_MAX
  884. SET_INPUT(Z2_MAX_PIN);
  885. #ifdef ENDSTOPPULLUP_ZMAX
  886. WRITE(Z2_MAX_PIN,HIGH);
  887. #endif
  888. #endif
  889. #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.
  890. SET_INPUT(Z_PROBE_PIN);
  891. #ifdef ENDSTOPPULLUP_ZPROBE
  892. WRITE(Z_PROBE_PIN,HIGH);
  893. #endif
  894. #endif
  895. #define AXIS_INIT(axis, AXIS, PIN) \
  896. AXIS ##_STEP_INIT; \
  897. AXIS ##_STEP_WRITE(INVERT_## PIN ##_STEP_PIN); \
  898. disable_## axis()
  899. #define E_AXIS_INIT(NUM) AXIS_INIT(e## NUM, E## NUM, E)
  900. // Initialize Step Pins
  901. #if HAS_X_STEP
  902. AXIS_INIT(x, X, X);
  903. #endif
  904. #if HAS_X2_STEP
  905. AXIS_INIT(x, X2, X);
  906. #endif
  907. #if HAS_Y_STEP
  908. #if defined(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_STEP
  909. Y2_STEP_INIT;
  910. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  911. #endif
  912. AXIS_INIT(y, Y, Y);
  913. #endif
  914. #if HAS_Z_STEP
  915. #if defined(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_STEP
  916. Z2_STEP_INIT;
  917. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  918. #endif
  919. AXIS_INIT(z, Z, Z);
  920. #endif
  921. #if HAS_E0_STEP
  922. E_AXIS_INIT(0);
  923. #endif
  924. #if HAS_E1_STEP
  925. E_AXIS_INIT(1);
  926. #endif
  927. #if HAS_E2_STEP
  928. E_AXIS_INIT(2);
  929. #endif
  930. #if HAS_E3_STEP
  931. E_AXIS_INIT(3);
  932. #endif
  933. // waveform generation = 0100 = CTC
  934. TCCR1B &= ~BIT(WGM13);
  935. TCCR1B |= BIT(WGM12);
  936. TCCR1A &= ~BIT(WGM11);
  937. TCCR1A &= ~BIT(WGM10);
  938. // output mode = 00 (disconnected)
  939. TCCR1A &= ~(3<<COM1A0);
  940. TCCR1A &= ~(3<<COM1B0);
  941. // Set the timer pre-scaler
  942. // Generally we use a divider of 8, resulting in a 2MHz timer
  943. // frequency on a 16MHz MCU. If you are going to change this, be
  944. // sure to regenerate speed_lookuptable.h with
  945. // create_speed_lookuptable.py
  946. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  947. OCR1A = 0x4000;
  948. TCNT1 = 0;
  949. ENABLE_STEPPER_DRIVER_INTERRUPT();
  950. #ifdef ADVANCE
  951. #if defined(TCCR0A) && defined(WGM01)
  952. TCCR0A &= ~BIT(WGM01);
  953. TCCR0A &= ~BIT(WGM00);
  954. #endif
  955. e_steps[0] = e_steps[1] = e_steps[2] = e_steps[3] = 0;
  956. TIMSK0 |= BIT(OCIE0A);
  957. #endif //ADVANCE
  958. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  959. sei();
  960. }
  961. // Block until all buffered steps are executed
  962. void st_synchronize() {
  963. while (blocks_queued()) {
  964. manage_heater();
  965. manage_inactivity();
  966. lcd_update();
  967. }
  968. }
  969. void st_set_position(const long &x, const long &y, const long &z, const long &e) {
  970. CRITICAL_SECTION_START;
  971. count_position[X_AXIS] = x;
  972. count_position[Y_AXIS] = y;
  973. count_position[Z_AXIS] = z;
  974. count_position[E_AXIS] = e;
  975. CRITICAL_SECTION_END;
  976. }
  977. void st_set_e_position(const long &e) {
  978. CRITICAL_SECTION_START;
  979. count_position[E_AXIS] = e;
  980. CRITICAL_SECTION_END;
  981. }
  982. long st_get_position(uint8_t axis) {
  983. long count_pos;
  984. CRITICAL_SECTION_START;
  985. count_pos = count_position[axis];
  986. CRITICAL_SECTION_END;
  987. return count_pos;
  988. }
  989. #ifdef ENABLE_AUTO_BED_LEVELING
  990. float st_get_position_mm(uint8_t axis) {
  991. float steper_position_in_steps = st_get_position(axis);
  992. return steper_position_in_steps / axis_steps_per_unit[axis];
  993. }
  994. #endif // ENABLE_AUTO_BED_LEVELING
  995. void finishAndDisableSteppers() {
  996. st_synchronize();
  997. disable_all_steppers();
  998. }
  999. void quickStop() {
  1000. cleaning_buffer_counter = 5000;
  1001. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1002. while (blocks_queued()) plan_discard_current_block();
  1003. current_block = NULL;
  1004. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1005. }
  1006. #ifdef BABYSTEPPING
  1007. // MUST ONLY BE CALLED BY AN ISR,
  1008. // No other ISR should ever interrupt this!
  1009. void babystep(const uint8_t axis, const bool direction) {
  1010. #define BABYSTEP_AXIS(axis, AXIS, INVERT) { \
  1011. enable_## axis(); \
  1012. uint8_t old_pin = AXIS ##_DIR_READ; \
  1013. AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR^direction^INVERT, true); \
  1014. AXIS ##_APPLY_STEP(!INVERT_## AXIS ##_STEP_PIN, true); \
  1015. delayMicroseconds(2); \
  1016. AXIS ##_APPLY_STEP(INVERT_## AXIS ##_STEP_PIN, true); \
  1017. AXIS ##_APPLY_DIR(old_pin, true); \
  1018. }
  1019. switch(axis) {
  1020. case X_AXIS:
  1021. BABYSTEP_AXIS(x, X, false);
  1022. break;
  1023. case Y_AXIS:
  1024. BABYSTEP_AXIS(y, Y, false);
  1025. break;
  1026. case Z_AXIS: {
  1027. #ifndef DELTA
  1028. BABYSTEP_AXIS(z, Z, BABYSTEP_INVERT_Z);
  1029. #else // DELTA
  1030. bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  1031. enable_x();
  1032. enable_y();
  1033. enable_z();
  1034. uint8_t old_x_dir_pin = X_DIR_READ,
  1035. old_y_dir_pin = Y_DIR_READ,
  1036. old_z_dir_pin = Z_DIR_READ;
  1037. //setup new step
  1038. X_DIR_WRITE(INVERT_X_DIR^z_direction);
  1039. Y_DIR_WRITE(INVERT_Y_DIR^z_direction);
  1040. Z_DIR_WRITE(INVERT_Z_DIR^z_direction);
  1041. //perform step
  1042. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  1043. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1044. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1045. delayMicroseconds(2);
  1046. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1047. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1048. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  1049. //get old pin state back.
  1050. X_DIR_WRITE(old_x_dir_pin);
  1051. Y_DIR_WRITE(old_y_dir_pin);
  1052. Z_DIR_WRITE(old_z_dir_pin);
  1053. #endif
  1054. } break;
  1055. default: break;
  1056. }
  1057. }
  1058. #endif //BABYSTEPPING
  1059. // From Arduino DigitalPotControl example
  1060. void digitalPotWrite(int address, int value) {
  1061. #if HAS_DIGIPOTSS
  1062. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1063. SPI.transfer(address); // send in the address and value via SPI:
  1064. SPI.transfer(value);
  1065. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1066. //delay(10);
  1067. #endif
  1068. }
  1069. // Initialize Digipot Motor Current
  1070. void digipot_init() {
  1071. #if HAS_DIGIPOTSS
  1072. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1073. SPI.begin();
  1074. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1075. for (int i = 0; i <= 4; i++) {
  1076. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1077. digipot_current(i,digipot_motor_current[i]);
  1078. }
  1079. #endif
  1080. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1081. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1082. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1083. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1084. digipot_current(0, motor_current_setting[0]);
  1085. digipot_current(1, motor_current_setting[1]);
  1086. digipot_current(2, motor_current_setting[2]);
  1087. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1088. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1089. #endif
  1090. }
  1091. void digipot_current(uint8_t driver, int current) {
  1092. #if HAS_DIGIPOTSS
  1093. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1094. digitalPotWrite(digipot_ch[driver], current);
  1095. #endif
  1096. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1097. switch(driver) {
  1098. case 0: analogWrite(MOTOR_CURRENT_PWM_XY_PIN, 255L * current / MOTOR_CURRENT_PWM_RANGE); break;
  1099. case 1: analogWrite(MOTOR_CURRENT_PWM_Z_PIN, 255L * current / MOTOR_CURRENT_PWM_RANGE); break;
  1100. case 2: analogWrite(MOTOR_CURRENT_PWM_E_PIN, 255L * current / MOTOR_CURRENT_PWM_RANGE); break;
  1101. }
  1102. #endif
  1103. }
  1104. void microstep_init() {
  1105. #if HAS_MICROSTEPS_E1
  1106. pinMode(E1_MS1_PIN,OUTPUT);
  1107. pinMode(E1_MS2_PIN,OUTPUT);
  1108. #endif
  1109. #if HAS_MICROSTEPS
  1110. pinMode(X_MS1_PIN,OUTPUT);
  1111. pinMode(X_MS2_PIN,OUTPUT);
  1112. pinMode(Y_MS1_PIN,OUTPUT);
  1113. pinMode(Y_MS2_PIN,OUTPUT);
  1114. pinMode(Z_MS1_PIN,OUTPUT);
  1115. pinMode(Z_MS2_PIN,OUTPUT);
  1116. pinMode(E0_MS1_PIN,OUTPUT);
  1117. pinMode(E0_MS2_PIN,OUTPUT);
  1118. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1119. for (uint16_t i = 0; i < sizeof(microstep_modes) / sizeof(microstep_modes[0]); i++)
  1120. microstep_mode(i, microstep_modes[i]);
  1121. #endif
  1122. }
  1123. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
  1124. if (ms1 >= 0) switch(driver) {
  1125. case 0: digitalWrite(X_MS1_PIN, ms1); break;
  1126. case 1: digitalWrite(Y_MS1_PIN, ms1); break;
  1127. case 2: digitalWrite(Z_MS1_PIN, ms1); break;
  1128. case 3: digitalWrite(E0_MS1_PIN, ms1); break;
  1129. #if HAS_MICROSTEPS_E1
  1130. case 4: digitalWrite(E1_MS1_PIN, ms1); break;
  1131. #endif
  1132. }
  1133. if (ms2 >= 0) switch(driver) {
  1134. case 0: digitalWrite(X_MS2_PIN, ms2); break;
  1135. case 1: digitalWrite(Y_MS2_PIN, ms2); break;
  1136. case 2: digitalWrite(Z_MS2_PIN, ms2); break;
  1137. case 3: digitalWrite(E0_MS2_PIN, ms2); break;
  1138. #if defined(E1_MS2_PIN) && E1_MS2_PIN >= 0
  1139. case 4: digitalWrite(E1_MS2_PIN, ms2); break;
  1140. #endif
  1141. }
  1142. }
  1143. void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
  1144. switch(stepping_mode) {
  1145. case 1: microstep_ms(driver,MICROSTEP1); break;
  1146. case 2: microstep_ms(driver,MICROSTEP2); break;
  1147. case 4: microstep_ms(driver,MICROSTEP4); break;
  1148. case 8: microstep_ms(driver,MICROSTEP8); break;
  1149. case 16: microstep_ms(driver,MICROSTEP16); break;
  1150. }
  1151. }
  1152. void microstep_readings() {
  1153. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1154. SERIAL_PROTOCOLPGM("X: ");
  1155. SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
  1156. SERIAL_PROTOCOLLN(digitalRead(X_MS2_PIN));
  1157. SERIAL_PROTOCOLPGM("Y: ");
  1158. SERIAL_PROTOCOL(digitalRead(Y_MS1_PIN));
  1159. SERIAL_PROTOCOLLN(digitalRead(Y_MS2_PIN));
  1160. SERIAL_PROTOCOLPGM("Z: ");
  1161. SERIAL_PROTOCOL(digitalRead(Z_MS1_PIN));
  1162. SERIAL_PROTOCOLLN(digitalRead(Z_MS2_PIN));
  1163. SERIAL_PROTOCOLPGM("E0: ");
  1164. SERIAL_PROTOCOL(digitalRead(E0_MS1_PIN));
  1165. SERIAL_PROTOCOLLN(digitalRead(E0_MS2_PIN));
  1166. #if HAS_MICROSTEPS_E1
  1167. SERIAL_PROTOCOLPGM("E1: ");
  1168. SERIAL_PROTOCOL(digitalRead(E1_MS1_PIN));
  1169. SERIAL_PROTOCOLLN(digitalRead(E1_MS2_PIN));
  1170. #endif
  1171. }
  1172. #ifdef Z_DUAL_ENDSTOPS
  1173. void In_Homing_Process(bool state) { performing_homing = state; }
  1174. void Lock_z_motor(bool state) { locked_z_motor = state; }
  1175. void Lock_z2_motor(bool state) { locked_z2_motor = state; }
  1176. #endif