My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

stepper.cpp 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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 defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  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 long counter_x, // Counter variables for the bresenham line tracer
  40. counter_y,
  41. counter_z,
  42. counter_e;
  43. volatile static unsigned long step_events_completed; // The number of step events executed in the current block
  44. #ifdef ADVANCE
  45. static long advance_rate, advance, final_advance = 0;
  46. static long old_advance = 0;
  47. static long e_steps[4];
  48. #endif
  49. static long acceleration_time, deceleration_time;
  50. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  51. static unsigned short acc_step_rate; // needed for deccelaration start point
  52. static char step_loops;
  53. static unsigned short OCR1A_nominal;
  54. static unsigned short step_loops_nominal;
  55. volatile long endstops_trigsteps[3]={0,0,0};
  56. volatile long endstops_stepsTotal,endstops_stepsDone;
  57. static volatile bool endstop_x_hit=false;
  58. static volatile bool endstop_y_hit=false;
  59. static volatile bool endstop_z_hit=false;
  60. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  61. bool abort_on_endstop_hit = false;
  62. #endif
  63. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  64. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  65. #endif
  66. static bool old_x_min_endstop=false;
  67. static bool old_x_max_endstop=false;
  68. static bool old_y_min_endstop=false;
  69. static bool old_y_max_endstop=false;
  70. static bool old_z_min_endstop=false;
  71. static bool old_z_max_endstop=false;
  72. static bool check_endstops = true;
  73. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  74. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  75. // Stepper objects of TMC steppers are used
  76. #ifdef X_IS_TMC
  77. TMC26XStepper stepperX(200,X_ENABLE_PIN,X_STEP_PIN,X_DIR_PIN,X_MAX_CURRENT,X_SENSE_RESISTOR);
  78. #endif
  79. #ifdef X2_IS_TMC
  80. TMC26XStepper stepperX2(200,X2_ENABLE_PIN,X2_STEP_PIN,X2_DIR_PIN,X2_MAX_CURRENT,X2_SENSE_RESISTOR);
  81. #endif
  82. #ifdef Y_IS_TMC
  83. TMC26XStepper stepperY(200,Y_ENABLE_PIN,Y_STEP_PIN,Y_DIR_PIN,Y_MAX_CURRENT,Y_SENSE_RESISTOR);
  84. #endif
  85. #ifdef Y2_IS_TMC
  86. TMC26XStepper stepperY2(200,Y2_ENABLE_PIN,Y2_STEP_PIN,Y2_DIR_PIN,Y2_MAX_CURRENT,Y2_SENSE_RESISTOR);
  87. #endif
  88. #ifdef Z_IS_TMC
  89. TMC26XStepper stepperZ(200,Z_ENABLE_PIN,Z_STEP_PIN,Z_DIR_PIN,Z_MAX_CURRENT,Z_SENSE_RESISTOR);
  90. #endif
  91. #ifdef Z2_IS_TMC
  92. TMC26XStepper stepperZ2(200,Z2_ENABLE_PIN,Z2_STEP_PIN,Z2_DIR_PIN,Z2_MAX_CURRENT,Z2_SENSE_RESISTOR);
  93. #endif
  94. #ifdef E0_IS_TMC
  95. TMC26XStepper stepperE0(200,E0_ENABLE_PIN,E0_STEP_PIN,E0_DIR_PIN,E0_MAX_CURRENT,E0_SENSE_RESISTOR);
  96. #endif
  97. #ifdef E1_IS_TMC
  98. TMC26XStepper stepperE1(200,E1_ENABLE_PIN,E1_STEP_PIN,E1_DIR_PIN,E1_MAX_CURRENT,E1_SENSE_RESISTOR);
  99. #endif
  100. #ifdef E2_IS_TMC
  101. TMC26XStepper stepperE2(200,E2_ENABLE_PIN,E2_STEP_PIN,E2_DIR_PIN,E2_MAX_CURRENT,E2_SENSE_RESISTOR);
  102. #endif
  103. #ifdef E3_IS_TMC
  104. TMC26XStepper stepperE3(200,E3_ENABLE_PIN,E3_STEP_PIN,E3_DIR_PIN,E3_MAX_CURRENT,E3_SENSE_RESISTOR);
  105. #endif
  106. //===========================================================================
  107. //=============================functions ============================
  108. //===========================================================================
  109. #define CHECK_ENDSTOPS if(check_endstops)
  110. // intRes = intIn1 * intIn2 >> 16
  111. // uses:
  112. // r26 to store 0
  113. // r27 to store the byte 1 of the 24 bit result
  114. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  115. asm volatile ( \
  116. "clr r26 \n\t" \
  117. "mul %A1, %B2 \n\t" \
  118. "movw %A0, r0 \n\t" \
  119. "mul %A1, %A2 \n\t" \
  120. "add %A0, r1 \n\t" \
  121. "adc %B0, r26 \n\t" \
  122. "lsr r0 \n\t" \
  123. "adc %A0, r26 \n\t" \
  124. "adc %B0, r26 \n\t" \
  125. "clr r1 \n\t" \
  126. : \
  127. "=&r" (intRes) \
  128. : \
  129. "d" (charIn1), \
  130. "d" (intIn2) \
  131. : \
  132. "r26" \
  133. )
  134. // intRes = longIn1 * longIn2 >> 24
  135. // uses:
  136. // r26 to store 0
  137. // r27 to store the byte 1 of the 48bit result
  138. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  139. asm volatile ( \
  140. "clr r26 \n\t" \
  141. "mul %A1, %B2 \n\t" \
  142. "mov r27, r1 \n\t" \
  143. "mul %B1, %C2 \n\t" \
  144. "movw %A0, r0 \n\t" \
  145. "mul %C1, %C2 \n\t" \
  146. "add %B0, r0 \n\t" \
  147. "mul %C1, %B2 \n\t" \
  148. "add %A0, r0 \n\t" \
  149. "adc %B0, r1 \n\t" \
  150. "mul %A1, %C2 \n\t" \
  151. "add r27, r0 \n\t" \
  152. "adc %A0, r1 \n\t" \
  153. "adc %B0, r26 \n\t" \
  154. "mul %B1, %B2 \n\t" \
  155. "add r27, r0 \n\t" \
  156. "adc %A0, r1 \n\t" \
  157. "adc %B0, r26 \n\t" \
  158. "mul %C1, %A2 \n\t" \
  159. "add r27, r0 \n\t" \
  160. "adc %A0, r1 \n\t" \
  161. "adc %B0, r26 \n\t" \
  162. "mul %B1, %A2 \n\t" \
  163. "add r27, r1 \n\t" \
  164. "adc %A0, r26 \n\t" \
  165. "adc %B0, r26 \n\t" \
  166. "lsr r27 \n\t" \
  167. "adc %A0, r26 \n\t" \
  168. "adc %B0, r26 \n\t" \
  169. "clr r1 \n\t" \
  170. : \
  171. "=&r" (intRes) \
  172. : \
  173. "d" (longIn1), \
  174. "d" (longIn2) \
  175. : \
  176. "r26" , "r27" \
  177. )
  178. // Some useful constants
  179. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  180. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  181. void checkHitEndstops()
  182. {
  183. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  184. SERIAL_ECHO_START;
  185. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  186. if(endstop_x_hit) {
  187. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  188. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "X");
  189. }
  190. if(endstop_y_hit) {
  191. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  192. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Y");
  193. }
  194. if(endstop_z_hit) {
  195. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  196. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
  197. }
  198. SERIAL_ECHOLN("");
  199. endstop_x_hit=false;
  200. endstop_y_hit=false;
  201. endstop_z_hit=false;
  202. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  203. if (abort_on_endstop_hit)
  204. {
  205. card.sdprinting = false;
  206. card.closefile();
  207. quickStop();
  208. setTargetHotend0(0);
  209. setTargetHotend1(0);
  210. setTargetHotend2(0);
  211. setTargetHotend3(0);
  212. setTargetBed(0);
  213. }
  214. #endif
  215. }
  216. }
  217. void endstops_hit_on_purpose()
  218. {
  219. endstop_x_hit=false;
  220. endstop_y_hit=false;
  221. endstop_z_hit=false;
  222. }
  223. void enable_endstops(bool check)
  224. {
  225. check_endstops = check;
  226. }
  227. // __________________________
  228. // /| |\ _________________ ^
  229. // / | | \ /| |\ |
  230. // / | | \ / | | \ s
  231. // / | | | | | \ p
  232. // / | | | | | \ e
  233. // +-----+------------------------+---+--+---------------+----+ e
  234. // | BLOCK 1 | BLOCK 2 | d
  235. //
  236. // time ----->
  237. //
  238. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  239. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  240. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  241. // The slope of acceleration is calculated with the leib ramp alghorithm.
  242. void st_wake_up() {
  243. // TCNT1 = 0;
  244. ENABLE_STEPPER_DRIVER_INTERRUPT();
  245. }
  246. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  247. unsigned short timer;
  248. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  249. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  250. step_rate = (step_rate >> 2)&0x3fff;
  251. step_loops = 4;
  252. }
  253. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  254. step_rate = (step_rate >> 1)&0x7fff;
  255. step_loops = 2;
  256. }
  257. else {
  258. step_loops = 1;
  259. }
  260. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  261. step_rate -= (F_CPU/500000); // Correct for minimal speed
  262. if(step_rate >= (8*256)){ // higher step rate
  263. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  264. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  265. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  266. MultiU16X8toH16(timer, tmp_step_rate, gain);
  267. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  268. }
  269. else { // lower step rates
  270. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  271. table_address += ((step_rate)>>1) & 0xfffc;
  272. timer = (unsigned short)pgm_read_word_near(table_address);
  273. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  274. }
  275. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  276. return timer;
  277. }
  278. // Initializes the trapezoid generator from the current block. Called whenever a new
  279. // block begins.
  280. FORCE_INLINE void trapezoid_generator_reset() {
  281. #ifdef ADVANCE
  282. advance = current_block->initial_advance;
  283. final_advance = current_block->final_advance;
  284. // Do E steps + advance steps
  285. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  286. old_advance = advance >>8;
  287. #endif
  288. deceleration_time = 0;
  289. // step_rate to timer interval
  290. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  291. // make a note of the number of step loops required at nominal speed
  292. step_loops_nominal = step_loops;
  293. acc_step_rate = current_block->initial_rate;
  294. acceleration_time = calc_timer(acc_step_rate);
  295. OCR1A = acceleration_time;
  296. // SERIAL_ECHO_START;
  297. // SERIAL_ECHOPGM("advance :");
  298. // SERIAL_ECHO(current_block->advance/256.0);
  299. // SERIAL_ECHOPGM("advance rate :");
  300. // SERIAL_ECHO(current_block->advance_rate/256.0);
  301. // SERIAL_ECHOPGM("initial advance :");
  302. // SERIAL_ECHO(current_block->initial_advance/256.0);
  303. // SERIAL_ECHOPGM("final advance :");
  304. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  305. }
  306. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  307. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  308. ISR(TIMER1_COMPA_vect)
  309. {
  310. // If there is no current block, attempt to pop one from the buffer
  311. if (current_block == NULL) {
  312. // Anything in the buffer?
  313. current_block = plan_get_current_block();
  314. if (current_block != NULL) {
  315. current_block->busy = true;
  316. trapezoid_generator_reset();
  317. counter_x = -(current_block->step_event_count >> 1);
  318. counter_y = counter_x;
  319. counter_z = counter_x;
  320. counter_e = counter_x;
  321. step_events_completed = 0;
  322. #ifdef Z_LATE_ENABLE
  323. if(current_block->steps_z > 0) {
  324. enable_z();
  325. OCR1A = 2000; //1ms wait
  326. return;
  327. }
  328. #endif
  329. // #ifdef ADVANCE
  330. // e_steps[current_block->active_extruder] = 0;
  331. // #endif
  332. }
  333. else {
  334. OCR1A=2000; // 1kHz.
  335. }
  336. }
  337. if (current_block != NULL) {
  338. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  339. out_bits = current_block->direction_bits;
  340. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  341. if((out_bits & (1<<X_AXIS))!=0){
  342. #ifdef DUAL_X_CARRIAGE
  343. if (extruder_duplication_enabled){
  344. X_DIR_WRITE(INVERT_X_DIR);
  345. X2_DIR_WRITE(INVERT_X_DIR);
  346. }
  347. else{
  348. if (current_block->active_extruder != 0)
  349. X2_DIR_WRITE(INVERT_X_DIR);
  350. else
  351. X_DIR_WRITE(INVERT_X_DIR);
  352. }
  353. #else
  354. X_DIR_WRITE(INVERT_X_DIR);
  355. #endif
  356. count_direction[X_AXIS]=-1;
  357. }
  358. else{
  359. #ifdef DUAL_X_CARRIAGE
  360. if (extruder_duplication_enabled){
  361. X_DIR_WRITE(!INVERT_X_DIR);
  362. X2_DIR_WRITE( !INVERT_X_DIR);
  363. }
  364. else{
  365. if (current_block->active_extruder != 0)
  366. X2_DIR_WRITE(!INVERT_X_DIR);
  367. else
  368. X_DIR_WRITE(!INVERT_X_DIR);
  369. }
  370. #else
  371. X_DIR_WRITE(!INVERT_X_DIR);
  372. #endif
  373. count_direction[X_AXIS]=1;
  374. }
  375. if((out_bits & (1<<Y_AXIS))!=0){
  376. Y_DIR_WRITE(INVERT_Y_DIR);
  377. #ifdef Y_DUAL_STEPPER_DRIVERS
  378. Y2_DIR_WRITE(!(INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  379. #endif
  380. count_direction[Y_AXIS]=-1;
  381. }
  382. else{
  383. Y_DIR_WRITE(!INVERT_Y_DIR);
  384. #ifdef Y_DUAL_STEPPER_DRIVERS
  385. Y2_DIR_WRITE((INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  386. #endif
  387. count_direction[Y_AXIS]=1;
  388. }
  389. // Set direction en check limit switches
  390. #ifndef COREXY
  391. if ((out_bits & (1<<X_AXIS)) != 0) // stepping along -X axis
  392. #else
  393. if ((out_bits & (1<<X_HEAD)) != 0) //AlexBorro: Head direction in -X axis for CoreXY bots.
  394. #endif
  395. {
  396. CHECK_ENDSTOPS
  397. {
  398. #ifdef DUAL_X_CARRIAGE
  399. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  400. if ((current_block->active_extruder == 0 && X_HOME_DIR == -1)
  401. || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
  402. #endif
  403. {
  404. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  405. bool x_min_endstop=(READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  406. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  407. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  408. endstop_x_hit=true;
  409. step_events_completed = current_block->step_event_count;
  410. }
  411. old_x_min_endstop = x_min_endstop;
  412. #endif
  413. }
  414. }
  415. }
  416. else
  417. { // +direction
  418. CHECK_ENDSTOPS
  419. {
  420. #ifdef DUAL_X_CARRIAGE
  421. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  422. if ((current_block->active_extruder == 0 && X_HOME_DIR == 1)
  423. || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
  424. #endif
  425. {
  426. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  427. bool x_max_endstop=(READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  428. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  429. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  430. endstop_x_hit=true;
  431. step_events_completed = current_block->step_event_count;
  432. }
  433. old_x_max_endstop = x_max_endstop;
  434. #endif
  435. }
  436. }
  437. }
  438. #ifndef COREXY
  439. if ((out_bits & (1<<Y_AXIS)) != 0) // -direction
  440. #else
  441. if ((out_bits & (1<<Y_HEAD)) != 0) //AlexBorro: Head direction in -Y axis for CoreXY bots.
  442. #endif
  443. {
  444. CHECK_ENDSTOPS
  445. {
  446. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  447. bool y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  448. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  449. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  450. endstop_y_hit=true;
  451. step_events_completed = current_block->step_event_count;
  452. }
  453. old_y_min_endstop = y_min_endstop;
  454. #endif
  455. }
  456. }
  457. else
  458. { // +direction
  459. CHECK_ENDSTOPS
  460. {
  461. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  462. bool y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  463. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  464. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  465. endstop_y_hit=true;
  466. step_events_completed = current_block->step_event_count;
  467. }
  468. old_y_max_endstop = y_max_endstop;
  469. #endif
  470. }
  471. }
  472. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  473. Z_DIR_WRITE(INVERT_Z_DIR);
  474. #ifdef Z_DUAL_STEPPER_DRIVERS
  475. Z2_DIR_WRITE(INVERT_Z_DIR);
  476. #endif
  477. count_direction[Z_AXIS]=-1;
  478. CHECK_ENDSTOPS
  479. {
  480. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  481. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  482. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  483. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  484. endstop_z_hit=true;
  485. step_events_completed = current_block->step_event_count;
  486. }
  487. old_z_min_endstop = z_min_endstop;
  488. #endif
  489. }
  490. }
  491. else { // +direction
  492. Z_DIR_WRITE(!INVERT_Z_DIR);
  493. #ifdef Z_DUAL_STEPPER_DRIVERS
  494. Z2_DIR_WRITE(!INVERT_Z_DIR);
  495. #endif
  496. count_direction[Z_AXIS]=1;
  497. CHECK_ENDSTOPS
  498. {
  499. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  500. bool z_max_endstop=(READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  501. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  502. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  503. endstop_z_hit=true;
  504. step_events_completed = current_block->step_event_count;
  505. }
  506. old_z_max_endstop = z_max_endstop;
  507. #endif
  508. }
  509. }
  510. #ifndef ADVANCE
  511. if ((out_bits & (1<<E_AXIS)) != 0) { // -direction
  512. REV_E_DIR();
  513. count_direction[E_AXIS]=-1;
  514. }
  515. else { // +direction
  516. NORM_E_DIR();
  517. count_direction[E_AXIS]=1;
  518. }
  519. #endif //!ADVANCE
  520. for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  521. #ifndef AT90USB
  522. MSerial.checkRx(); // Check for serial chars.
  523. #endif
  524. #ifdef ADVANCE
  525. counter_e += current_block->steps_e;
  526. if (counter_e > 0) {
  527. counter_e -= current_block->step_event_count;
  528. if ((out_bits & (1<<E_AXIS)) != 0) { // - direction
  529. e_steps[current_block->active_extruder]--;
  530. }
  531. else {
  532. e_steps[current_block->active_extruder]++;
  533. }
  534. }
  535. #endif //ADVANCE
  536. counter_x += current_block->steps_x;
  537. #ifdef CONFIG_STEPPERS_TOSHIBA
  538. /* The toshiba stepper controller require much longer pulses
  539. * tjerfore we 'stage' decompose the pulses between high, and
  540. * low instead of doing each in turn. The extra tests add enough
  541. * lag to allow it work with without needing NOPs */
  542. if (counter_x > 0) {
  543. X_STEP_WRITE(HIGH);
  544. }
  545. counter_y += current_block->steps_y;
  546. if (counter_y > 0) {
  547. Y_STEP_WRITE( HIGH);
  548. }
  549. counter_z += current_block->steps_z;
  550. if (counter_z > 0) {
  551. Z_STEP_WRITE( HIGH);
  552. }
  553. #ifndef ADVANCE
  554. counter_e += current_block->steps_e;
  555. if (counter_e > 0) {
  556. WRITE_E_STEP(HIGH);
  557. }
  558. #endif //!ADVANCE
  559. if (counter_x > 0) {
  560. counter_x -= current_block->step_event_count;
  561. count_position[X_AXIS]+=count_direction[X_AXIS];
  562. X_STEP_WRITE(LOW);
  563. }
  564. if (counter_y > 0) {
  565. counter_y -= current_block->step_event_count;
  566. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  567. Y_STEP_WRITE( LOW);
  568. }
  569. if (counter_z > 0) {
  570. counter_z -= current_block->step_event_count;
  571. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  572. Z_STEP_WRITE(LOW);
  573. }
  574. #ifndef ADVANCE
  575. if (counter_e > 0) {
  576. counter_e -= current_block->step_event_count;
  577. count_position[E_AXIS]+=count_direction[E_AXIS];
  578. WRITE_E_STEP(LOW);
  579. }
  580. #endif //!ADVANCE
  581. #else
  582. if (counter_x > 0) {
  583. #ifdef DUAL_X_CARRIAGE
  584. if (extruder_duplication_enabled){
  585. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  586. X2_STEP_WRITE( !INVERT_X_STEP_PIN);
  587. }
  588. else {
  589. if (current_block->active_extruder != 0)
  590. X2_STEP_WRITE( !INVERT_X_STEP_PIN);
  591. else
  592. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  593. }
  594. #else
  595. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  596. #endif
  597. counter_x -= current_block->step_event_count;
  598. count_position[X_AXIS]+=count_direction[X_AXIS];
  599. #ifdef DUAL_X_CARRIAGE
  600. if (extruder_duplication_enabled){
  601. X_STEP_WRITE(INVERT_X_STEP_PIN);
  602. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  603. }
  604. else {
  605. if (current_block->active_extruder != 0)
  606. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  607. else
  608. X_STEP_WRITE(INVERT_X_STEP_PIN);
  609. }
  610. #else
  611. X_STEP_WRITE(INVERT_X_STEP_PIN);
  612. #endif
  613. }
  614. counter_y += current_block->steps_y;
  615. if (counter_y > 0) {
  616. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  617. #ifdef Y_DUAL_STEPPER_DRIVERS
  618. Y2_STEP_WRITE( !INVERT_Y_STEP_PIN);
  619. #endif
  620. counter_y -= current_block->step_event_count;
  621. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  622. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  623. #ifdef Y_DUAL_STEPPER_DRIVERS
  624. Y2_STEP_WRITE( INVERT_Y_STEP_PIN);
  625. #endif
  626. }
  627. counter_z += current_block->steps_z;
  628. if (counter_z > 0) {
  629. Z_STEP_WRITE( !INVERT_Z_STEP_PIN);
  630. #ifdef Z_DUAL_STEPPER_DRIVERS
  631. Z2_STEP_WRITE(!INVERT_Z_STEP_PIN);
  632. #endif
  633. counter_z -= current_block->step_event_count;
  634. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  635. Z_STEP_WRITE( INVERT_Z_STEP_PIN);
  636. #ifdef Z_DUAL_STEPPER_DRIVERS
  637. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  638. #endif
  639. }
  640. #ifndef ADVANCE
  641. counter_e += current_block->steps_e;
  642. if (counter_e > 0) {
  643. WRITE_E_STEP(!INVERT_E_STEP_PIN);
  644. counter_e -= current_block->step_event_count;
  645. count_position[E_AXIS]+=count_direction[E_AXIS];
  646. WRITE_E_STEP(INVERT_E_STEP_PIN);
  647. }
  648. #endif //!ADVANCE
  649. #endif // CONFIG_STEPPERS_TOSHIBA
  650. step_events_completed += 1;
  651. if(step_events_completed >= current_block->step_event_count) break;
  652. }
  653. // Calculare new timer value
  654. unsigned short timer;
  655. unsigned short step_rate;
  656. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  657. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  658. acc_step_rate += current_block->initial_rate;
  659. // upper limit
  660. if(acc_step_rate > current_block->nominal_rate)
  661. acc_step_rate = current_block->nominal_rate;
  662. // step_rate to timer interval
  663. timer = calc_timer(acc_step_rate);
  664. OCR1A = timer;
  665. acceleration_time += timer;
  666. #ifdef ADVANCE
  667. for(int8_t i=0; i < step_loops; i++) {
  668. advance += advance_rate;
  669. }
  670. //if(advance > current_block->advance) advance = current_block->advance;
  671. // Do E steps + advance steps
  672. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  673. old_advance = advance >>8;
  674. #endif
  675. }
  676. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  677. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  678. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  679. step_rate = current_block->final_rate;
  680. }
  681. else {
  682. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  683. }
  684. // lower limit
  685. if(step_rate < current_block->final_rate)
  686. step_rate = current_block->final_rate;
  687. // step_rate to timer interval
  688. timer = calc_timer(step_rate);
  689. OCR1A = timer;
  690. deceleration_time += timer;
  691. #ifdef ADVANCE
  692. for(int8_t i=0; i < step_loops; i++) {
  693. advance -= advance_rate;
  694. }
  695. if(advance < final_advance) advance = final_advance;
  696. // Do E steps + advance steps
  697. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  698. old_advance = advance >>8;
  699. #endif //ADVANCE
  700. }
  701. else {
  702. OCR1A = OCR1A_nominal;
  703. // ensure we're running at the correct step rate, even if we just came off an acceleration
  704. step_loops = step_loops_nominal;
  705. }
  706. // If current block is finished, reset pointer
  707. if (step_events_completed >= current_block->step_event_count) {
  708. current_block = NULL;
  709. plan_discard_current_block();
  710. }
  711. }
  712. }
  713. #ifdef ADVANCE
  714. unsigned char old_OCR0A;
  715. // Timer interrupt for E. e_steps is set in the main routine;
  716. // Timer 0 is shared with millies
  717. ISR(TIMER0_COMPA_vect)
  718. {
  719. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  720. OCR0A = old_OCR0A;
  721. // Set E direction (Depends on E direction + advance)
  722. for(unsigned char i=0; i<4;i++) {
  723. if (e_steps[0] != 0) {
  724. E0_STEP_WRITE( INVERT_E_STEP_PIN);
  725. if (e_steps[0] < 0) {
  726. E0_DIR_WRITE(INVERT_E0_DIR);
  727. e_steps[0]++;
  728. E0_STEP_WRITE(!INVERT_E_STEP_PIN);
  729. }
  730. else if (e_steps[0] > 0) {
  731. E0_DIR_WRITE(!INVERT_E0_DIR);
  732. e_steps[0]--;
  733. E0_STEP_WRITE(!INVERT_E_STEP_PIN);
  734. }
  735. }
  736. #if EXTRUDERS > 1
  737. if (e_steps[1] != 0) {
  738. E1_STEP_WRITE(INVERT_E_STEP_PIN);
  739. if (e_steps[1] < 0) {
  740. E1_DIR_WRITE(INVERT_E1_DIR);
  741. e_steps[1]++;
  742. E1_STEP_WRITE(!INVERT_E_STEP_PIN);
  743. }
  744. else if (e_steps[1] > 0) {
  745. E1_DIR_WRITE(!INVERT_E1_DIR);
  746. e_steps[1]--;
  747. E1_STEP_WRITE(!INVERT_E_STEP_PIN);
  748. }
  749. }
  750. #endif
  751. #if EXTRUDERS > 2
  752. if (e_steps[2] != 0) {
  753. E2_STEP_WRITE(INVERT_E_STEP_PIN);
  754. if (e_steps[2] < 0) {
  755. E2_DIR_WRITE(INVERT_E2_DIR);
  756. e_steps[2]++;
  757. E2_STEP_WRITE(!INVERT_E_STEP_PIN);
  758. }
  759. else if (e_steps[2] > 0) {
  760. E2_DIR_WRITE(!INVERT_E2_DIR);
  761. e_steps[2]--;
  762. E2_STEP_WRITE(!INVERT_E_STEP_PIN);
  763. }
  764. }
  765. #endif
  766. #if EXTRUDERS > 3
  767. if (e_steps[3] != 0) {
  768. E3_STEP_WRITE(INVERT_E_STEP_PIN);
  769. if (e_steps[3] < 0) {
  770. E3_DIR_WRITE(INVERT_E3_DIR);
  771. e_steps[3]++;
  772. E3_STEP_WRITE(!INVERT_E_STEP_PIN);
  773. }
  774. else if (e_steps[3] > 0) {
  775. E3_DIR_WRITE(!INVERT_E3_DIR);
  776. e_steps[3]--;
  777. E3_STEP_WRITE(!INVERT_E_STEP_PIN);
  778. }
  779. }
  780. #endif
  781. }
  782. }
  783. #endif // ADVANCE
  784. void st_init()
  785. {
  786. digipot_init(); //Initialize Digipot Motor Current
  787. microstep_init(); //Initialize Microstepping Pins
  788. // initialise TMC Steppers
  789. #ifdef HAVE_TMCDRIVER
  790. tmc_init();
  791. #endif
  792. //Initialize Dir Pins
  793. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  794. X_DIR_INIT;
  795. #endif
  796. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  797. X2_DIR_INIT;
  798. #endif
  799. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  800. Y_DIR_INIT;
  801. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  802. Y2_DIR_INIT;
  803. #endif
  804. #endif
  805. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  806. Z_DIR_INIT;
  807. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  808. Z2_DIR_INIT;
  809. #endif
  810. #endif
  811. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  812. E0_DIR_INIT;
  813. #endif
  814. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  815. E1_DIR_INIT;
  816. #endif
  817. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  818. E2_DIR_INIT;
  819. #endif
  820. #if defined(E3_DIR_PIN) && (E3_DIR_PIN > -1)
  821. E3_DIR_INIT;
  822. #endif
  823. //Initialize Enable Pins - steppers default to disabled.
  824. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  825. X_ENABLE_INIT;
  826. if(!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  827. #endif
  828. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  829. X2_ENABLE_INIT;
  830. if(!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  831. #endif
  832. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  833. Y_ENABLE_INIT;
  834. if(!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  835. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  836. Y2_ENABLE_INIT;
  837. if(!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  838. #endif
  839. #endif
  840. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  841. Z_ENABLE_INIT;
  842. if(!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  843. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  844. Z2_ENABLE_INIT;
  845. if(!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  846. #endif
  847. #endif
  848. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  849. E0_ENABLE_INIT;
  850. if(!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  851. #endif
  852. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  853. E1_ENABLE_INIT;
  854. if(!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  855. #endif
  856. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  857. E2_ENABLE_INIT;
  858. if(!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  859. #endif
  860. #if defined(E3_ENABLE_PIN) && (E3_ENABLE_PIN > -1)
  861. E3_ENABLE_INIT;
  862. if(!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  863. #endif
  864. //endstops and pullups
  865. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  866. SET_INPUT(X_MIN_PIN);
  867. #ifdef ENDSTOPPULLUP_XMIN
  868. WRITE(X_MIN_PIN,HIGH);
  869. #endif
  870. #endif
  871. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  872. SET_INPUT(Y_MIN_PIN);
  873. #ifdef ENDSTOPPULLUP_YMIN
  874. WRITE(Y_MIN_PIN,HIGH);
  875. #endif
  876. #endif
  877. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  878. SET_INPUT(Z_MIN_PIN);
  879. #ifdef ENDSTOPPULLUP_ZMIN
  880. WRITE(Z_MIN_PIN,HIGH);
  881. #endif
  882. #endif
  883. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  884. SET_INPUT(X_MAX_PIN);
  885. #ifdef ENDSTOPPULLUP_XMAX
  886. WRITE(X_MAX_PIN,HIGH);
  887. #endif
  888. #endif
  889. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  890. SET_INPUT(Y_MAX_PIN);
  891. #ifdef ENDSTOPPULLUP_YMAX
  892. WRITE(Y_MAX_PIN,HIGH);
  893. #endif
  894. #endif
  895. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  896. SET_INPUT(Z_MAX_PIN);
  897. #ifdef ENDSTOPPULLUP_ZMAX
  898. WRITE(Z_MAX_PIN,HIGH);
  899. #endif
  900. #endif
  901. //Initialize Step Pins
  902. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  903. X_STEP_INIT;
  904. X_STEP_WRITE(INVERT_X_STEP_PIN);
  905. disable_x();
  906. #endif
  907. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  908. X2_STEP_INIT;
  909. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  910. disable_x();
  911. #endif
  912. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  913. Y_STEP_INIT;
  914. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  915. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  916. Y2_STEP_INIT;
  917. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  918. #endif
  919. disable_y();
  920. #endif
  921. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  922. Z_STEP_INIT;
  923. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  924. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  925. Z2_STEP_INIT;
  926. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  927. #endif
  928. disable_z();
  929. #endif
  930. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  931. E0_STEP_INIT;
  932. E0_STEP_WRITE(INVERT_E_STEP_PIN);
  933. disable_e0();
  934. #endif
  935. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  936. E1_STEP_INIT;
  937. E1_STEP_WRITE(INVERT_E_STEP_PIN);
  938. disable_e1();
  939. #endif
  940. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  941. E2_STEP_INIT;
  942. E2_STEP_WRITE(INVERT_E_STEP_PIN);
  943. disable_e2();
  944. #endif
  945. #if defined(E3_STEP_PIN) && (E3_STEP_PIN > -1)
  946. E3_STEP_INIT;
  947. E3_STEP_WRITE(INVERT_E_STEP_PIN);
  948. disable_e3();
  949. #endif
  950. // waveform generation = 0100 = CTC
  951. TCCR1B &= ~(1<<WGM13);
  952. TCCR1B |= (1<<WGM12);
  953. TCCR1A &= ~(1<<WGM11);
  954. TCCR1A &= ~(1<<WGM10);
  955. // output mode = 00 (disconnected)
  956. TCCR1A &= ~(3<<COM1A0);
  957. TCCR1A &= ~(3<<COM1B0);
  958. // Set the timer pre-scaler
  959. // Generally we use a divider of 8, resulting in a 2MHz timer
  960. // frequency on a 16MHz MCU. If you are going to change this, be
  961. // sure to regenerate speed_lookuptable.h with
  962. // create_speed_lookuptable.py
  963. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  964. OCR1A = 0x4000;
  965. TCNT1 = 0;
  966. ENABLE_STEPPER_DRIVER_INTERRUPT();
  967. #ifdef ADVANCE
  968. #if defined(TCCR0A) && defined(WGM01)
  969. TCCR0A &= ~(1<<WGM01);
  970. TCCR0A &= ~(1<<WGM00);
  971. #endif
  972. e_steps[0] = 0;
  973. e_steps[1] = 0;
  974. e_steps[2] = 0;
  975. e_steps[3] = 0;
  976. TIMSK0 |= (1<<OCIE0A);
  977. #endif //ADVANCE
  978. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  979. sei();
  980. }
  981. // Block until all buffered steps are executed
  982. void st_synchronize()
  983. {
  984. while( blocks_queued()) {
  985. manage_heater();
  986. manage_inactivity();
  987. lcd_update();
  988. }
  989. }
  990. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  991. {
  992. CRITICAL_SECTION_START;
  993. count_position[X_AXIS] = x;
  994. count_position[Y_AXIS] = y;
  995. count_position[Z_AXIS] = z;
  996. count_position[E_AXIS] = e;
  997. CRITICAL_SECTION_END;
  998. }
  999. void st_set_e_position(const long &e)
  1000. {
  1001. CRITICAL_SECTION_START;
  1002. count_position[E_AXIS] = e;
  1003. CRITICAL_SECTION_END;
  1004. }
  1005. long st_get_position(uint8_t axis)
  1006. {
  1007. long count_pos;
  1008. CRITICAL_SECTION_START;
  1009. count_pos = count_position[axis];
  1010. CRITICAL_SECTION_END;
  1011. return count_pos;
  1012. }
  1013. #ifdef ENABLE_AUTO_BED_LEVELING
  1014. float st_get_position_mm(uint8_t axis)
  1015. {
  1016. float steper_position_in_steps = st_get_position(axis);
  1017. return steper_position_in_steps / axis_steps_per_unit[axis];
  1018. }
  1019. #endif // ENABLE_AUTO_BED_LEVELING
  1020. void finishAndDisableSteppers()
  1021. {
  1022. st_synchronize();
  1023. disable_x();
  1024. disable_y();
  1025. disable_z();
  1026. disable_e0();
  1027. disable_e1();
  1028. disable_e2();
  1029. disable_e3();
  1030. }
  1031. void quickStop()
  1032. {
  1033. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1034. while(blocks_queued())
  1035. plan_discard_current_block();
  1036. current_block = NULL;
  1037. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1038. }
  1039. #ifdef BABYSTEPPING
  1040. void babystep(const uint8_t axis,const bool direction)
  1041. {
  1042. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  1043. //store initial pin states
  1044. switch(axis)
  1045. {
  1046. case X_AXIS:
  1047. {
  1048. enable_x();
  1049. uint8_t old_x_dir_pin= X_DIR_READ; //if dualzstepper, both point to same direction.
  1050. //setup new step
  1051. X_DIR_WRITE((INVERT_X_DIR)^direction);
  1052. #ifdef DUAL_X_CARRIAGE
  1053. X2_DIR_WRITE((INVERT_X_DIR)^direction);
  1054. #endif
  1055. //perform step
  1056. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  1057. #ifdef DUAL_X_CARRIAGE
  1058. X2_STEP_WRITE(!INVERT_X_STEP_PIN);
  1059. #endif
  1060. _delay_us(1U); // wait 1 microsecond
  1061. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1062. #ifdef DUAL_X_CARRIAGE
  1063. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  1064. #endif
  1065. //get old pin state back.
  1066. X_DIR_WRITE(old_x_dir_pin);
  1067. #ifdef DUAL_X_CARRIAGE
  1068. X2_DIR_WRITE(old_x_dir_pin);
  1069. #endif
  1070. }
  1071. break;
  1072. case Y_AXIS:
  1073. {
  1074. enable_y();
  1075. uint8_t old_y_dir_pin= Y_DIR_READ; //if dualzstepper, both point to same direction.
  1076. //setup new step
  1077. Y_DIR_WRITE((INVERT_Y_DIR)^direction);
  1078. #ifdef DUAL_Y_CARRIAGE
  1079. Y2_DIR_WRITE((INVERT_Y_DIR)^direction);
  1080. #endif
  1081. //perform step
  1082. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1083. #ifdef DUAL_Y_CARRIAGE
  1084. Y2_STEP_WRITE( !INVERT_Y_STEP_PIN);
  1085. #endif
  1086. _delay_us(1U); // wait 1 microsecond
  1087. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1088. #ifdef DUAL_Y_CARRIAGE
  1089. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  1090. #endif
  1091. //get old pin state back.
  1092. Y_DIR_WRITE(old_y_dir_pin);
  1093. #ifdef DUAL_Y_CARRIAGE
  1094. Y2_DIR_WRITE(old_y_dir_pin);
  1095. #endif
  1096. }
  1097. break;
  1098. #ifndef DELTA
  1099. case Z_AXIS:
  1100. {
  1101. enable_z();
  1102. uint8_t old_z_dir_pin= Z_DIR_READ; //if dualzstepper, both point to same direction.
  1103. //setup new step
  1104. Z_DIR_WRITE((INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1105. #ifdef Z_DUAL_STEPPER_DRIVERS
  1106. Z2_DIR_WRITE((INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1107. #endif
  1108. //perform step
  1109. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1110. #ifdef Z_DUAL_STEPPER_DRIVERS
  1111. Z2_STEP_WRITE( !INVERT_Z_STEP_PIN);
  1112. #endif
  1113. _delay_us(1U); // wait 1 microsecond
  1114. Z_STEP_WRITE( INVERT_Z_STEP_PIN);
  1115. #ifdef Z_DUAL_STEPPER_DRIVERS
  1116. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  1117. #endif
  1118. //get old pin state back.
  1119. Z_DIR_WRITE(old_z_dir_pin);
  1120. #ifdef Z_DUAL_STEPPER_DRIVERS
  1121. Z2_DIR_WRITE(old_z_dir_pin);
  1122. #endif
  1123. }
  1124. break;
  1125. #else //DELTA
  1126. case Z_AXIS:
  1127. {
  1128. enable_x();
  1129. enable_y();
  1130. enable_z();
  1131. uint8_t old_x_dir_pin= X_DIR_READ;
  1132. uint8_t old_y_dir_pin= Y_DIR_READ;
  1133. uint8_t old_z_dir_pin= Z_DIR_READ;
  1134. //setup new step
  1135. X_DIR_WRITE((INVERT_X_DIR)^direction^BABYSTEP_INVERT_Z);
  1136. Y_DIR_WRITE((INVERT_Y_DIR)^direction^BABYSTEP_INVERT_Z);
  1137. Z_DIR_WRITE((INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1138. //perform step
  1139. X_STEP_WRITE( !INVERT_X_STEP_PIN);
  1140. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1141. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1142. _delay_us(1U); // wait 1 microsecond
  1143. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1144. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1145. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  1146. //get old pin state back.
  1147. X_DIR_WRITE(old_x_dir_pin);
  1148. Y_DIR_WRITE(old_y_dir_pin);
  1149. Z_DIR_WRITE(old_z_dir_pin);
  1150. }
  1151. break;
  1152. #endif
  1153. default: break;
  1154. }
  1155. }
  1156. #endif //BABYSTEPPING
  1157. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  1158. {
  1159. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1160. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1161. SPI.transfer(address); // send in the address and value via SPI:
  1162. SPI.transfer(value);
  1163. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1164. //delay(10);
  1165. #endif
  1166. }
  1167. void digipot_init() //Initialize Digipot Motor Current
  1168. {
  1169. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1170. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1171. SPI.begin();
  1172. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1173. for(int i=0;i<=4;i++)
  1174. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1175. digipot_current(i,digipot_motor_current[i]);
  1176. #endif
  1177. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1178. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1179. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1180. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1181. digipot_current(0, motor_current_setting[0]);
  1182. digipot_current(1, motor_current_setting[1]);
  1183. digipot_current(2, motor_current_setting[2]);
  1184. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1185. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1186. #endif
  1187. }
  1188. void digipot_current(uint8_t driver, int current)
  1189. {
  1190. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1191. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1192. digitalPotWrite(digipot_ch[driver], current);
  1193. #endif
  1194. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1195. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1196. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1197. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1198. #endif
  1199. }
  1200. void microstep_init()
  1201. {
  1202. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1203. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1204. pinMode(E1_MS1_PIN,OUTPUT);
  1205. pinMode(E1_MS2_PIN,OUTPUT);
  1206. #endif
  1207. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1208. pinMode(X_MS1_PIN,OUTPUT);
  1209. pinMode(X_MS2_PIN,OUTPUT);
  1210. pinMode(Y_MS1_PIN,OUTPUT);
  1211. pinMode(Y_MS2_PIN,OUTPUT);
  1212. pinMode(Z_MS1_PIN,OUTPUT);
  1213. pinMode(Z_MS2_PIN,OUTPUT);
  1214. pinMode(E0_MS1_PIN,OUTPUT);
  1215. pinMode(E0_MS2_PIN,OUTPUT);
  1216. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1217. #endif
  1218. }
  1219. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1220. {
  1221. if(ms1 > -1) switch(driver)
  1222. {
  1223. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1224. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1225. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1226. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1227. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1228. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1229. #endif
  1230. }
  1231. if(ms2 > -1) switch(driver)
  1232. {
  1233. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1234. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1235. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1236. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1237. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1238. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1239. #endif
  1240. }
  1241. }
  1242. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1243. {
  1244. switch(stepping_mode)
  1245. {
  1246. case 1: microstep_ms(driver,MICROSTEP1); break;
  1247. case 2: microstep_ms(driver,MICROSTEP2); break;
  1248. case 4: microstep_ms(driver,MICROSTEP4); break;
  1249. case 8: microstep_ms(driver,MICROSTEP8); break;
  1250. case 16: microstep_ms(driver,MICROSTEP16); break;
  1251. }
  1252. }
  1253. void microstep_readings()
  1254. {
  1255. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1256. SERIAL_PROTOCOLPGM("X: ");
  1257. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1258. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1259. SERIAL_PROTOCOLPGM("Y: ");
  1260. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1261. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1262. SERIAL_PROTOCOLPGM("Z: ");
  1263. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1264. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1265. SERIAL_PROTOCOLPGM("E0: ");
  1266. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1267. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1268. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1269. SERIAL_PROTOCOLPGM("E1: ");
  1270. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1271. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1272. #endif
  1273. }
  1274. #ifdef HAVE_TMCDRIVER
  1275. void tmc_init()
  1276. {
  1277. #ifdef X_IS_TMC
  1278. stepperX.setMicrosteps(X_MICROSTEPS);
  1279. stepperX.start();
  1280. #endif
  1281. #ifdef X2_IS_TMC
  1282. stepperX2.setMicrosteps(X2_MICROSTEPS);
  1283. stepperX2.start();
  1284. #endif
  1285. #ifdef Y_IS_TMC
  1286. stepperY.setMicrosteps(Y_MICROSTEPS);
  1287. stepperY.start();
  1288. #endif
  1289. #ifdef Y2_IS_TMC
  1290. stepperY2.setMicrosteps(Y2_MICROSTEPS);
  1291. stepperY2.start();
  1292. #endif
  1293. #ifdef Z_IS_TMC
  1294. stepperZ.setMicrosteps(Z_MICROSTEPS);
  1295. stepperZ.start();
  1296. #endif
  1297. #ifdef Z2_IS_TMC
  1298. stepperZ2.setMicrosteps(Z2_MICROSTEPS);
  1299. stepperZ2.start();
  1300. #endif
  1301. #ifdef E0_IS_TMC
  1302. stepperE0.setMicrosteps(E0_MICROSTEPS);
  1303. stepperE0.start();
  1304. #endif
  1305. #ifdef E1_IS_TMC
  1306. stepperE1.setMicrosteps(E1_MICROSTEPS);
  1307. stepperE1.start();
  1308. #endif
  1309. #ifdef E2_IS_TMC
  1310. stepperE2.setMicrosteps(E2_MICROSTEPS);
  1311. stepperE2.start();
  1312. #endif
  1313. #ifdef E3_IS_TMC
  1314. stepperE3.setMicrosteps(E3_MICROSTEPS);
  1315. stepperE3.start();
  1316. #endif
  1317. }
  1318. #endif