stepper.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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. #ifdef TMC2130
  30. #include "tmc2130.h"
  31. #endif //TMC2130
  32. //===========================================================================
  33. //=============================public variables ============================
  34. //===========================================================================
  35. block_t *current_block; // A pointer to the block currently being traced
  36. bool x_min_endstop = false;
  37. bool x_max_endstop = false;
  38. bool y_min_endstop = false;
  39. bool y_max_endstop = false;
  40. bool z_min_endstop = false;
  41. bool z_max_endstop = false;
  42. //===========================================================================
  43. //=============================private variables ============================
  44. //===========================================================================
  45. //static makes it inpossible to be called from outside of this file by extern.!
  46. // Variables used by The Stepper Driver Interrupt
  47. static unsigned char out_bits; // The next stepping-bits to be output
  48. static int32_t counter_x, // Counter variables for the bresenham line tracer
  49. counter_y,
  50. counter_z,
  51. counter_e;
  52. volatile static uint32_t step_events_completed; // The number of step events executed in the current block
  53. static int32_t acceleration_time, deceleration_time;
  54. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  55. static uint16_t acc_step_rate; // needed for deccelaration start point
  56. static uint8_t step_loops;
  57. static uint16_t OCR1A_nominal;
  58. static uint8_t step_loops_nominal;
  59. volatile long endstops_trigsteps[3]={0,0,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. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  65. bool abort_on_endstop_hit = false;
  66. #endif
  67. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  68. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  69. int motor_current_setting_silent[3] = DEFAULT_PWM_MOTOR_CURRENT;
  70. int motor_current_setting_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  71. #endif
  72. static bool old_x_min_endstop=false;
  73. static bool old_x_max_endstop=false;
  74. static bool old_y_min_endstop=false;
  75. static bool old_y_max_endstop=false;
  76. static bool old_z_min_endstop=false;
  77. static bool old_z_max_endstop=false;
  78. static bool check_endstops = true;
  79. static bool check_z_endstop = false;
  80. int8_t SilentMode;
  81. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  82. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  83. #ifdef LIN_ADVANCE
  84. uint16_t ADV_NEVER = 65535;
  85. static uint16_t nextMainISR = 0;
  86. static uint16_t nextAdvanceISR = ADV_NEVER;
  87. static uint16_t eISR_Rate = ADV_NEVER;
  88. static volatile int e_steps; //Extrusion steps to be executed by the stepper
  89. static int final_estep_rate; //Speed of extruder at cruising speed
  90. static int current_estep_rate; //The current speed of the extruder
  91. static int current_adv_steps; //The current pretension of filament expressed in steps
  92. #define ADV_RATE(T, L) (e_steps ? (T) * (L) / abs(e_steps) : ADV_NEVER)
  93. #define _NEXT_ISR(T) nextMainISR = T
  94. #else
  95. #define _NEXT_ISR(T) OCR1A = T
  96. #endif
  97. //===========================================================================
  98. //=============================functions ============================
  99. //===========================================================================
  100. #define CHECK_ENDSTOPS if(check_endstops)
  101. // intRes = intIn1 * intIn2 >> 16
  102. // uses:
  103. // r26 to store 0
  104. // r27 to store the byte 1 of the 24 bit result
  105. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  106. asm volatile ( \
  107. "clr r26 \n\t" \
  108. "mul %A1, %B2 \n\t" \
  109. "movw %A0, r0 \n\t" \
  110. "mul %A1, %A2 \n\t" \
  111. "add %A0, r1 \n\t" \
  112. "adc %B0, r26 \n\t" \
  113. "lsr r0 \n\t" \
  114. "adc %A0, r26 \n\t" \
  115. "adc %B0, r26 \n\t" \
  116. "clr r1 \n\t" \
  117. : \
  118. "=&r" (intRes) \
  119. : \
  120. "d" (charIn1), \
  121. "d" (intIn2) \
  122. : \
  123. "r26" \
  124. )
  125. // intRes = longIn1 * longIn2 >> 24
  126. // uses:
  127. // r26 to store 0
  128. // r27 to store the byte 1 of the 48bit result
  129. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  130. asm volatile ( \
  131. "clr r26 \n\t" \
  132. "mul %A1, %B2 \n\t" \
  133. "mov r27, r1 \n\t" \
  134. "mul %B1, %C2 \n\t" \
  135. "movw %A0, r0 \n\t" \
  136. "mul %C1, %C2 \n\t" \
  137. "add %B0, r0 \n\t" \
  138. "mul %C1, %B2 \n\t" \
  139. "add %A0, r0 \n\t" \
  140. "adc %B0, r1 \n\t" \
  141. "mul %A1, %C2 \n\t" \
  142. "add r27, r0 \n\t" \
  143. "adc %A0, r1 \n\t" \
  144. "adc %B0, r26 \n\t" \
  145. "mul %B1, %B2 \n\t" \
  146. "add r27, r0 \n\t" \
  147. "adc %A0, r1 \n\t" \
  148. "adc %B0, r26 \n\t" \
  149. "mul %C1, %A2 \n\t" \
  150. "add r27, r0 \n\t" \
  151. "adc %A0, r1 \n\t" \
  152. "adc %B0, r26 \n\t" \
  153. "mul %B1, %A2 \n\t" \
  154. "add r27, r1 \n\t" \
  155. "adc %A0, r26 \n\t" \
  156. "adc %B0, r26 \n\t" \
  157. "lsr r27 \n\t" \
  158. "adc %A0, r26 \n\t" \
  159. "adc %B0, r26 \n\t" \
  160. "clr r1 \n\t" \
  161. : \
  162. "=&r" (intRes) \
  163. : \
  164. "d" (longIn1), \
  165. "d" (longIn2) \
  166. : \
  167. "r26" , "r27" \
  168. )
  169. // Some useful constants
  170. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  171. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  172. void checkHitEndstops()
  173. {
  174. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  175. SERIAL_ECHO_START;
  176. SERIAL_ECHORPGM(MSG_ENDSTOPS_HIT);
  177. if(endstop_x_hit) {
  178. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  179. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("X")));
  180. }
  181. if(endstop_y_hit) {
  182. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  183. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("Y")));
  184. }
  185. if(endstop_z_hit) {
  186. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  187. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT,PSTR("Z")));
  188. }
  189. SERIAL_ECHOLN("");
  190. endstop_x_hit=false;
  191. endstop_y_hit=false;
  192. endstop_z_hit=false;
  193. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  194. if (abort_on_endstop_hit)
  195. {
  196. card.sdprinting = false;
  197. card.closefile();
  198. quickStop();
  199. setTargetHotend0(0);
  200. setTargetHotend1(0);
  201. setTargetHotend2(0);
  202. }
  203. #endif
  204. }
  205. }
  206. bool endstops_hit_on_purpose()
  207. {
  208. bool hit = endstop_x_hit || endstop_y_hit || endstop_z_hit;
  209. endstop_x_hit=false;
  210. endstop_y_hit=false;
  211. endstop_z_hit=false;
  212. return hit;
  213. }
  214. bool endstop_z_hit_on_purpose()
  215. {
  216. bool hit = endstop_z_hit;
  217. endstop_z_hit=false;
  218. return hit;
  219. }
  220. bool enable_endstops(bool check)
  221. {
  222. bool old = check_endstops;
  223. check_endstops = check;
  224. return old;
  225. }
  226. bool enable_z_endstop(bool check)
  227. {
  228. bool old = check_z_endstop;
  229. check_z_endstop = check;
  230. endstop_z_hit=false;
  231. return old;
  232. }
  233. // __________________________
  234. // /| |\ _________________ ^
  235. // / | | \ /| |\ |
  236. // / | | \ / | | \ s
  237. // / | | | | | \ p
  238. // / | | | | | \ e
  239. // +-----+------------------------+---+--+---------------+----+ e
  240. // | BLOCK 1 | BLOCK 2 | d
  241. //
  242. // time ----->
  243. //
  244. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  245. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  246. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  247. // The slope of acceleration is calculated with the leib ramp alghorithm.
  248. void st_wake_up() {
  249. // TCNT1 = 0;
  250. ENABLE_STEPPER_DRIVER_INTERRUPT();
  251. }
  252. void step_wait(){
  253. for(int8_t i=0; i < 6; i++){
  254. }
  255. }
  256. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  257. unsigned short timer;
  258. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  259. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  260. step_rate = (step_rate >> 2)&0x3fff;
  261. step_loops = 4;
  262. }
  263. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  264. step_rate = (step_rate >> 1)&0x7fff;
  265. step_loops = 2;
  266. }
  267. else {
  268. step_loops = 1;
  269. }
  270. // step_loops = 1;
  271. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  272. step_rate -= (F_CPU/500000); // Correct for minimal speed
  273. if(step_rate >= (8*256)){ // higher step rate
  274. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  275. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  276. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  277. MultiU16X8toH16(timer, tmp_step_rate, gain);
  278. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  279. }
  280. else { // lower step rates
  281. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  282. table_address += ((step_rate)>>1) & 0xfffc;
  283. timer = (unsigned short)pgm_read_word_near(table_address);
  284. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  285. }
  286. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  287. return timer;
  288. }
  289. // Initializes the trapezoid generator from the current block. Called whenever a new
  290. // block begins.
  291. FORCE_INLINE void trapezoid_generator_reset() {
  292. deceleration_time = 0;
  293. // step_rate to timer interval
  294. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  295. // make a note of the number of step loops required at nominal speed
  296. step_loops_nominal = step_loops;
  297. acc_step_rate = current_block->initial_rate;
  298. acceleration_time = calc_timer(acc_step_rate);
  299. _NEXT_ISR(acceleration_time);
  300. #ifdef LIN_ADVANCE
  301. if (current_block->use_advance_lead) {
  302. current_estep_rate = ((unsigned long)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  303. final_estep_rate = (current_block->nominal_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  304. }
  305. #endif
  306. }
  307. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  308. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  309. ISR(TIMER1_COMPA_vect) {
  310. #ifdef LIN_ADVANCE
  311. advance_isr_scheduler();
  312. #else
  313. isr();
  314. #endif
  315. }
  316. void isr() {
  317. //if (UVLO) uvlo();
  318. // If there is no current block, attempt to pop one from the buffer
  319. if (current_block == NULL) {
  320. // Anything in the buffer?
  321. current_block = plan_get_current_block();
  322. if (current_block != NULL) {
  323. // The busy flag is set by the plan_get_current_block() call.
  324. // current_block->busy = true;
  325. trapezoid_generator_reset();
  326. counter_x = -(current_block->step_event_count >> 1);
  327. counter_y = counter_x;
  328. counter_z = counter_x;
  329. counter_e = counter_x;
  330. step_events_completed = 0;
  331. #ifdef Z_LATE_ENABLE
  332. if(current_block->steps_z > 0) {
  333. enable_z();
  334. _NEXT_ISR(2000); //1ms wait
  335. return;
  336. }
  337. #endif
  338. }
  339. else {
  340. _NEXT_ISR(2000); // 1kHz.
  341. }
  342. }
  343. if (current_block != NULL) {
  344. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  345. out_bits = current_block->direction_bits;
  346. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  347. if((out_bits & (1<<X_AXIS))!=0){
  348. WRITE(X_DIR_PIN, INVERT_X_DIR);
  349. count_direction[X_AXIS]=-1;
  350. }
  351. else{
  352. WRITE(X_DIR_PIN, !INVERT_X_DIR);
  353. count_direction[X_AXIS]=1;
  354. }
  355. if((out_bits & (1<<Y_AXIS))!=0){
  356. WRITE(Y_DIR_PIN, INVERT_Y_DIR);
  357. #ifdef Y_DUAL_STEPPER_DRIVERS
  358. WRITE(Y2_DIR_PIN, !(INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  359. #endif
  360. count_direction[Y_AXIS]=-1;
  361. }
  362. else{
  363. WRITE(Y_DIR_PIN, !INVERT_Y_DIR);
  364. #ifdef Y_DUAL_STEPPER_DRIVERS
  365. WRITE(Y2_DIR_PIN, (INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  366. #endif
  367. count_direction[Y_AXIS]=1;
  368. }
  369. // Set direction en check limit switches
  370. #ifndef COREXY
  371. if ((out_bits & (1<<X_AXIS)) != 0) { // stepping along -X axis
  372. #else
  373. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) { //-X occurs for -A and -B
  374. #endif
  375. CHECK_ENDSTOPS
  376. {
  377. {
  378. #if defined(X_MIN_PIN) && (X_MIN_PIN > -1) && !defined(DEBUG_DISABLE_XMINLIMIT)
  379. #ifndef TMC2130_SG_HOMING_SW_XY
  380. x_min_endstop = (READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  381. #else //TMC2130_SG_HOMING_SW_XY
  382. x_min_endstop = tmc2130_axis_stalled[X_AXIS];
  383. #endif //TMC2130_SG_HOMING_SW_XY
  384. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  385. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  386. endstop_x_hit=true;
  387. step_events_completed = current_block->step_event_count;
  388. }
  389. old_x_min_endstop = x_min_endstop;
  390. #endif
  391. }
  392. }
  393. }
  394. else { // +direction
  395. CHECK_ENDSTOPS
  396. {
  397. {
  398. #if defined(X_MAX_PIN) && (X_MAX_PIN > -1) && !defined(DEBUG_DISABLE_XMAXLIMIT)
  399. #ifndef TMC2130_SG_HOMING_SW_XY
  400. x_max_endstop = (READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  401. #else //TMC2130_SG_HOMING_SW_XY
  402. x_max_endstop = tmc2130_axis_stalled[X_AXIS];
  403. #endif //TMC2130_SG_HOMING_SW_XY
  404. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  405. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  406. endstop_x_hit=true;
  407. step_events_completed = current_block->step_event_count;
  408. }
  409. old_x_max_endstop = x_max_endstop;
  410. #endif
  411. }
  412. }
  413. }
  414. #ifndef COREXY
  415. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  416. #else
  417. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) { // -Y occurs for -A and +B
  418. #endif
  419. CHECK_ENDSTOPS
  420. {
  421. #if defined(Y_MIN_PIN) && (Y_MIN_PIN > -1) && !defined(DEBUG_DISABLE_YMINLIMIT)
  422. #ifndef TMC2130_SG_HOMING_SW_XY
  423. y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  424. #else //TMC2130_SG_HOMING_SW_XY
  425. y_min_endstop = tmc2130_axis_stalled[Y_AXIS];
  426. #endif //TMC2130_SG_HOMING_SW_XY
  427. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  428. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  429. endstop_y_hit=true;
  430. step_events_completed = current_block->step_event_count;
  431. }
  432. old_y_min_endstop = y_min_endstop;
  433. #endif
  434. }
  435. }
  436. else { // +direction
  437. CHECK_ENDSTOPS
  438. {
  439. #if defined(Y_MAX_PIN) && (Y_MAX_PIN > -1) && !defined(DEBUG_DISABLE_YMAXLIMIT)
  440. #ifndef TMC2130_SG_HOMING_SW_XY
  441. y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  442. #else //TMC2130_SG_HOMING_SW_XY
  443. y_max_endstop = tmc2130_axis_stalled[Y_AXIS];
  444. #endif //TMC2130_SG_HOMING_SW_XY
  445. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  446. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  447. endstop_y_hit=true;
  448. step_events_completed = current_block->step_event_count;
  449. }
  450. old_y_max_endstop = y_max_endstop;
  451. #endif
  452. }
  453. }
  454. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  455. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  456. #ifdef Z_DUAL_STEPPER_DRIVERS
  457. WRITE(Z2_DIR_PIN,INVERT_Z_DIR);
  458. #endif
  459. count_direction[Z_AXIS]=-1;
  460. if(check_endstops && ! check_z_endstop)
  461. {
  462. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  463. z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  464. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  465. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  466. endstop_z_hit=true;
  467. step_events_completed = current_block->step_event_count;
  468. }
  469. old_z_min_endstop = z_min_endstop;
  470. #endif
  471. }
  472. }
  473. else { // +direction
  474. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  475. #ifdef Z_DUAL_STEPPER_DRIVERS
  476. WRITE(Z2_DIR_PIN,!INVERT_Z_DIR);
  477. #endif
  478. count_direction[Z_AXIS]=1;
  479. CHECK_ENDSTOPS
  480. {
  481. #if defined(Z_MAX_PIN) && (Z_MAX_PIN > -1) && !defined(DEBUG_DISABLE_ZMAXLIMIT)
  482. #ifndef TMC2130_SG_HOMING_SW_Z
  483. z_max_endstop = (READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  484. #else //TMC2130_SG_HOMING_SW_Z
  485. z_max_endstop = tmc2130_axis_stalled[Z_AXIS];
  486. #endif //TMC2130_SG_HOMING_SW_Z
  487. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  488. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  489. endstop_z_hit=true;
  490. step_events_completed = current_block->step_event_count;
  491. }
  492. old_z_max_endstop = z_max_endstop;
  493. #endif
  494. }
  495. }
  496. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  497. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  498. if(check_z_endstop) {
  499. // Check the Z min end-stop no matter what.
  500. // Good for searching for the center of an induction target.
  501. z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  502. if(z_min_endstop && old_z_min_endstop) {
  503. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  504. endstop_z_hit=true;
  505. step_events_completed = current_block->step_event_count;
  506. }
  507. old_z_min_endstop = z_min_endstop;
  508. }
  509. #endif
  510. if ((out_bits & (1 << E_AXIS)) != 0)
  511. { // -direction
  512. //AKU
  513. #ifdef SNMM
  514. if (snmm_extruder == 0 || snmm_extruder == 2)
  515. {
  516. NORM_E_DIR();
  517. }
  518. else
  519. {
  520. REV_E_DIR();
  521. }
  522. #else
  523. REV_E_DIR();
  524. #endif // SNMM
  525. count_direction[E_AXIS] = -1;
  526. }
  527. else
  528. { // +direction
  529. #ifdef SNMM
  530. if (snmm_extruder == 0 || snmm_extruder == 2)
  531. {
  532. REV_E_DIR();
  533. }
  534. else
  535. {
  536. NORM_E_DIR();
  537. }
  538. #else
  539. NORM_E_DIR();
  540. #endif // SNMM
  541. count_direction[E_AXIS] = 1;
  542. }
  543. for(uint8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  544. #ifndef AT90USB
  545. MSerial.checkRx(); // Check for serial chars.
  546. #endif
  547. #ifdef LIN_ADVANCE
  548. counter_e += current_block->steps_e;
  549. if (counter_e > 0) {
  550. counter_e -= current_block->step_event_count;
  551. count_position[E_AXIS] += count_direction[E_AXIS];
  552. ((out_bits&(1<<E_AXIS))!=0) ? --e_steps : ++e_steps;
  553. }
  554. #endif
  555. counter_x += current_block->steps_x;
  556. if (counter_x > 0) {
  557. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  558. #ifdef DEBUG_XSTEP_DUP_PIN
  559. WRITE(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  560. #endif //DEBUG_XSTEP_DUP_PIN
  561. counter_x -= current_block->step_event_count;
  562. count_position[X_AXIS]+=count_direction[X_AXIS];
  563. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  564. #ifdef DEBUG_XSTEP_DUP_PIN
  565. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  566. #endif //DEBUG_XSTEP_DUP_PIN
  567. }
  568. counter_y += current_block->steps_y;
  569. if (counter_y > 0) {
  570. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  571. #ifdef DEBUG_YSTEP_DUP_PIN
  572. WRITE(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  573. #endif //DEBUG_YSTEP_DUP_PIN
  574. #ifdef Y_DUAL_STEPPER_DRIVERS
  575. WRITE(Y2_STEP_PIN, !INVERT_Y_STEP_PIN);
  576. #endif
  577. counter_y -= current_block->step_event_count;
  578. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  579. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  580. #ifdef DEBUG_YSTEP_DUP_PIN
  581. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  582. #endif //DEBUG_YSTEP_DUP_PIN
  583. #ifdef Y_DUAL_STEPPER_DRIVERS
  584. WRITE(Y2_STEP_PIN, INVERT_Y_STEP_PIN);
  585. #endif
  586. }
  587. counter_z += current_block->steps_z;
  588. if (counter_z > 0) {
  589. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  590. #ifdef Z_DUAL_STEPPER_DRIVERS
  591. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  592. #endif
  593. counter_z -= current_block->step_event_count;
  594. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  595. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  596. #ifdef Z_DUAL_STEPPER_DRIVERS
  597. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  598. #endif
  599. }
  600. #ifndef LIN_ADVANCE
  601. counter_e += current_block->steps_e;
  602. if (counter_e > 0) {
  603. WRITE_E_STEP(!INVERT_E_STEP_PIN);
  604. counter_e -= current_block->step_event_count;
  605. count_position[E_AXIS]+=count_direction[E_AXIS];
  606. WRITE_E_STEP(INVERT_E_STEP_PIN);
  607. }
  608. #endif
  609. step_events_completed += 1;
  610. if(step_events_completed >= current_block->step_event_count) break;
  611. }
  612. #ifdef LIN_ADVANCE
  613. if (current_block->use_advance_lead) {
  614. const int delta_adv_steps = current_estep_rate - current_adv_steps;
  615. current_adv_steps += delta_adv_steps;
  616. e_steps += delta_adv_steps;
  617. }
  618. // If we have esteps to execute, fire the next advance_isr "now"
  619. if (e_steps) nextAdvanceISR = 0;
  620. #endif
  621. // Calculare new timer value
  622. unsigned short timer;
  623. unsigned short step_rate;
  624. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  625. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  626. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  627. acc_step_rate += current_block->initial_rate;
  628. // upper limit
  629. if(acc_step_rate > current_block->nominal_rate)
  630. acc_step_rate = current_block->nominal_rate;
  631. // step_rate to timer interval
  632. timer = calc_timer(acc_step_rate);
  633. _NEXT_ISR(timer);
  634. acceleration_time += timer;
  635. #ifdef LIN_ADVANCE
  636. if (current_block->use_advance_lead) {
  637. current_estep_rate = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  638. }
  639. eISR_Rate = ADV_RATE(timer, step_loops);
  640. #endif
  641. }
  642. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  643. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  644. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  645. step_rate = current_block->final_rate;
  646. }
  647. else {
  648. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  649. }
  650. // lower limit
  651. if(step_rate < current_block->final_rate)
  652. step_rate = current_block->final_rate;
  653. // step_rate to timer interval
  654. timer = calc_timer(step_rate);
  655. _NEXT_ISR(timer);
  656. deceleration_time += timer;
  657. #ifdef LIN_ADVANCE
  658. if (current_block->use_advance_lead) {
  659. current_estep_rate = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  660. }
  661. eISR_Rate = ADV_RATE(timer, step_loops);
  662. #endif
  663. }
  664. else {
  665. #ifdef LIN_ADVANCE
  666. if (current_block->use_advance_lead)
  667. current_estep_rate = final_estep_rate;
  668. eISR_Rate = ADV_RATE(OCR1A_nominal, step_loops_nominal);
  669. #endif
  670. _NEXT_ISR(OCR1A_nominal);
  671. // ensure we're running at the correct step rate, even if we just came off an acceleration
  672. step_loops = step_loops_nominal;
  673. }
  674. // If current block is finished, reset pointer
  675. if (step_events_completed >= current_block->step_event_count) {
  676. current_block = NULL;
  677. plan_discard_current_block();
  678. }
  679. }
  680. check_fans();
  681. }
  682. #ifdef LIN_ADVANCE
  683. // Timer interrupt for E. e_steps is set in the main routine.
  684. void advance_isr() {
  685. nextAdvanceISR = eISR_Rate;
  686. if (e_steps) {
  687. bool dir =
  688. #ifdef SNMM
  689. ((e_steps < 0) == (snmm_extruder & 1))
  690. #else
  691. (e_steps < 0)
  692. #endif
  693. ? INVERT_E0_DIR : !INVERT_E0_DIR; //If we have SNMM, reverse every second extruder.
  694. WRITE(E0_DIR_PIN, dir);
  695. for (uint8_t i = step_loops; e_steps && i--;) {
  696. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  697. e_steps < 0 ? ++e_steps : --e_steps;
  698. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  699. }
  700. }
  701. }
  702. void advance_isr_scheduler() {
  703. // Run main stepping ISR if flagged
  704. if (!nextMainISR) isr();
  705. // Run Advance stepping ISR if flagged
  706. if (!nextAdvanceISR) advance_isr();
  707. // Is the next advance ISR scheduled before the next main ISR?
  708. if (nextAdvanceISR <= nextMainISR) {
  709. // Set up the next interrupt
  710. OCR1A = nextAdvanceISR;
  711. // New interval for the next main ISR
  712. if (nextMainISR) nextMainISR -= nextAdvanceISR;
  713. // Will call Stepper::advance_isr on the next interrupt
  714. nextAdvanceISR = 0;
  715. }
  716. else {
  717. // The next main ISR comes first
  718. OCR1A = nextMainISR;
  719. // New interval for the next advance ISR, if any
  720. if (nextAdvanceISR && nextAdvanceISR != ADV_NEVER)
  721. nextAdvanceISR -= nextMainISR;
  722. // Will call Stepper::isr on the next interrupt
  723. nextMainISR = 0;
  724. }
  725. // Don't run the ISR faster than possible
  726. if (OCR1A < TCNT1 + 16) OCR1A = TCNT1 + 16;
  727. }
  728. void clear_current_adv_vars() {
  729. e_steps = 0; //Should be already 0 at an filament change event, but just to be sure..
  730. current_adv_steps = 0;
  731. }
  732. #endif // LIN_ADVANCE
  733. void st_init()
  734. {
  735. #ifdef TMC2130
  736. tmc2130_init();
  737. #endif //TMC2130
  738. digipot_init(); //Initialize Digipot Motor Current
  739. microstep_init(); //Initialize Microstepping Pins
  740. //Initialize Dir Pins
  741. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  742. SET_OUTPUT(X_DIR_PIN);
  743. #endif
  744. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  745. SET_OUTPUT(X2_DIR_PIN);
  746. #endif
  747. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  748. SET_OUTPUT(Y_DIR_PIN);
  749. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  750. SET_OUTPUT(Y2_DIR_PIN);
  751. #endif
  752. #endif
  753. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  754. SET_OUTPUT(Z_DIR_PIN);
  755. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  756. SET_OUTPUT(Z2_DIR_PIN);
  757. #endif
  758. #endif
  759. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  760. SET_OUTPUT(E0_DIR_PIN);
  761. #endif
  762. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  763. SET_OUTPUT(E1_DIR_PIN);
  764. #endif
  765. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  766. SET_OUTPUT(E2_DIR_PIN);
  767. #endif
  768. //Initialize Enable Pins - steppers default to disabled.
  769. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  770. SET_OUTPUT(X_ENABLE_PIN);
  771. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  772. #endif
  773. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  774. SET_OUTPUT(X2_ENABLE_PIN);
  775. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  776. #endif
  777. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  778. SET_OUTPUT(Y_ENABLE_PIN);
  779. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  780. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  781. SET_OUTPUT(Y2_ENABLE_PIN);
  782. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  783. #endif
  784. #endif
  785. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  786. SET_OUTPUT(Z_ENABLE_PIN);
  787. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  788. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  789. SET_OUTPUT(Z2_ENABLE_PIN);
  790. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  791. #endif
  792. #endif
  793. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  794. SET_OUTPUT(E0_ENABLE_PIN);
  795. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  796. #endif
  797. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  798. SET_OUTPUT(E1_ENABLE_PIN);
  799. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  800. #endif
  801. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  802. SET_OUTPUT(E2_ENABLE_PIN);
  803. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  804. #endif
  805. //endstops and pullups
  806. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  807. SET_INPUT(X_MIN_PIN);
  808. #ifdef ENDSTOPPULLUP_XMIN
  809. WRITE(X_MIN_PIN,HIGH);
  810. #endif
  811. #endif
  812. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  813. SET_INPUT(Y_MIN_PIN);
  814. #ifdef ENDSTOPPULLUP_YMIN
  815. WRITE(Y_MIN_PIN,HIGH);
  816. #endif
  817. #endif
  818. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  819. SET_INPUT(Z_MIN_PIN);
  820. #ifdef ENDSTOPPULLUP_ZMIN
  821. WRITE(Z_MIN_PIN,HIGH);
  822. #endif
  823. #endif
  824. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  825. SET_INPUT(X_MAX_PIN);
  826. #ifdef ENDSTOPPULLUP_XMAX
  827. WRITE(X_MAX_PIN,HIGH);
  828. #endif
  829. #endif
  830. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  831. SET_INPUT(Y_MAX_PIN);
  832. #ifdef ENDSTOPPULLUP_YMAX
  833. WRITE(Y_MAX_PIN,HIGH);
  834. #endif
  835. #endif
  836. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  837. SET_INPUT(Z_MAX_PIN);
  838. #ifdef ENDSTOPPULLUP_ZMAX
  839. WRITE(Z_MAX_PIN,HIGH);
  840. #endif
  841. #endif
  842. //Initialize Step Pins
  843. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  844. SET_OUTPUT(X_STEP_PIN);
  845. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  846. #ifdef DEBUG_XSTEP_DUP_PIN
  847. SET_OUTPUT(DEBUG_XSTEP_DUP_PIN);
  848. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  849. #endif //DEBUG_XSTEP_DUP_PIN
  850. disable_x();
  851. #endif
  852. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  853. SET_OUTPUT(X2_STEP_PIN);
  854. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  855. disable_x();
  856. #endif
  857. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  858. SET_OUTPUT(Y_STEP_PIN);
  859. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  860. #ifdef DEBUG_YSTEP_DUP_PIN
  861. SET_OUTPUT(DEBUG_YSTEP_DUP_PIN);
  862. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  863. #endif //DEBUG_YSTEP_DUP_PIN
  864. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  865. SET_OUTPUT(Y2_STEP_PIN);
  866. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  867. #endif
  868. disable_y();
  869. #endif
  870. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  871. SET_OUTPUT(Z_STEP_PIN);
  872. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  873. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  874. SET_OUTPUT(Z2_STEP_PIN);
  875. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  876. #endif
  877. disable_z();
  878. #endif
  879. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  880. SET_OUTPUT(E0_STEP_PIN);
  881. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  882. disable_e0();
  883. #endif
  884. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  885. SET_OUTPUT(E1_STEP_PIN);
  886. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  887. disable_e1();
  888. #endif
  889. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  890. SET_OUTPUT(E2_STEP_PIN);
  891. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  892. disable_e2();
  893. #endif
  894. // waveform generation = 0100 = CTC
  895. TCCR1B &= ~(1<<WGM13);
  896. TCCR1B |= (1<<WGM12);
  897. TCCR1A &= ~(1<<WGM11);
  898. TCCR1A &= ~(1<<WGM10);
  899. // output mode = 00 (disconnected)
  900. TCCR1A &= ~(3<<COM1A0);
  901. TCCR1A &= ~(3<<COM1B0);
  902. // Set the timer pre-scaler
  903. // Generally we use a divider of 8, resulting in a 2MHz timer
  904. // frequency on a 16MHz MCU. If you are going to change this, be
  905. // sure to regenerate speed_lookuptable.h with
  906. // create_speed_lookuptable.py
  907. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  908. OCR1A = 0x4000;
  909. TCNT1 = 0;
  910. ENABLE_STEPPER_DRIVER_INTERRUPT();
  911. #ifdef LIN_ADVANCE
  912. e_steps = 0;
  913. current_adv_steps = 0;
  914. #endif
  915. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  916. sei();
  917. }
  918. // Block until all buffered steps are executed
  919. void st_synchronize()
  920. {
  921. while(blocks_queued())
  922. {
  923. #ifdef TMC2130
  924. manage_heater();
  925. // Vojtech: Don't disable motors inside the planner!
  926. if (!tmc2130_update_sg())
  927. {
  928. manage_inactivity(true);
  929. lcd_update();
  930. }
  931. #else //TMC2130
  932. manage_heater();
  933. // Vojtech: Don't disable motors inside the planner!
  934. manage_inactivity(true);
  935. lcd_update();
  936. #endif //TMC2130
  937. }
  938. }
  939. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  940. {
  941. CRITICAL_SECTION_START;
  942. count_position[X_AXIS] = x;
  943. count_position[Y_AXIS] = y;
  944. count_position[Z_AXIS] = z;
  945. count_position[E_AXIS] = e;
  946. CRITICAL_SECTION_END;
  947. }
  948. void st_set_e_position(const long &e)
  949. {
  950. CRITICAL_SECTION_START;
  951. count_position[E_AXIS] = e;
  952. CRITICAL_SECTION_END;
  953. }
  954. long st_get_position(uint8_t axis)
  955. {
  956. long count_pos;
  957. CRITICAL_SECTION_START;
  958. count_pos = count_position[axis];
  959. CRITICAL_SECTION_END;
  960. return count_pos;
  961. }
  962. void st_get_position_xy(long &x, long &y)
  963. {
  964. CRITICAL_SECTION_START;
  965. x = count_position[X_AXIS];
  966. y = count_position[Y_AXIS];
  967. CRITICAL_SECTION_END;
  968. }
  969. float st_get_position_mm(uint8_t axis)
  970. {
  971. float steper_position_in_steps = st_get_position(axis);
  972. return steper_position_in_steps / axis_steps_per_unit[axis];
  973. }
  974. void finishAndDisableSteppers()
  975. {
  976. st_synchronize();
  977. disable_x();
  978. disable_y();
  979. disable_z();
  980. disable_e0();
  981. disable_e1();
  982. disable_e2();
  983. }
  984. void quickStop()
  985. {
  986. DISABLE_STEPPER_DRIVER_INTERRUPT();
  987. while (blocks_queued()) plan_discard_current_block();
  988. current_block = NULL;
  989. ENABLE_STEPPER_DRIVER_INTERRUPT();
  990. }
  991. #ifdef BABYSTEPPING
  992. void babystep(const uint8_t axis,const bool direction)
  993. {
  994. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  995. //store initial pin states
  996. switch(axis)
  997. {
  998. case X_AXIS:
  999. {
  1000. enable_x();
  1001. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  1002. //setup new step
  1003. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  1004. //perform step
  1005. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  1006. #ifdef DEBUG_XSTEP_DUP_PIN
  1007. WRITE(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  1008. #endif //DEBUG_XSTEP_DUP_PIN
  1009. {
  1010. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1011. }
  1012. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  1013. #ifdef DEBUG_XSTEP_DUP_PIN
  1014. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  1015. #endif //DEBUG_XSTEP_DUP_PIN
  1016. //get old pin state back.
  1017. WRITE(X_DIR_PIN,old_x_dir_pin);
  1018. }
  1019. break;
  1020. case Y_AXIS:
  1021. {
  1022. enable_y();
  1023. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  1024. //setup new step
  1025. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  1026. //perform step
  1027. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  1028. #ifdef DEBUG_YSTEP_DUP_PIN
  1029. WRITE(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  1030. #endif //DEBUG_YSTEP_DUP_PIN
  1031. {
  1032. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1033. }
  1034. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  1035. #ifdef DEBUG_YSTEP_DUP_PIN
  1036. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1037. #endif //DEBUG_YSTEP_DUP_PIN
  1038. //get old pin state back.
  1039. WRITE(Y_DIR_PIN,old_y_dir_pin);
  1040. }
  1041. break;
  1042. case Z_AXIS:
  1043. {
  1044. enable_z();
  1045. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  1046. //setup new step
  1047. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1048. #ifdef Z_DUAL_STEPPER_DRIVERS
  1049. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1050. #endif
  1051. //perform step
  1052. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  1053. #ifdef Z_DUAL_STEPPER_DRIVERS
  1054. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  1055. #endif
  1056. //wait a tiny bit
  1057. {
  1058. volatile float x=1./float(axis+1); //absolutely useless
  1059. }
  1060. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  1061. #ifdef Z_DUAL_STEPPER_DRIVERS
  1062. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  1063. #endif
  1064. //get old pin state back.
  1065. WRITE(Z_DIR_PIN,old_z_dir_pin);
  1066. #ifdef Z_DUAL_STEPPER_DRIVERS
  1067. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  1068. #endif
  1069. }
  1070. break;
  1071. default: break;
  1072. }
  1073. }
  1074. #endif //BABYSTEPPING
  1075. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  1076. {
  1077. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1078. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1079. SPI.transfer(address); // send in the address and value via SPI:
  1080. SPI.transfer(value);
  1081. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1082. //delay(10);
  1083. #endif
  1084. }
  1085. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  1086. {
  1087. do
  1088. {
  1089. *value = eeprom_read_byte((unsigned char*)pos);
  1090. pos++;
  1091. value++;
  1092. }while(--size);
  1093. }
  1094. void digipot_init() //Initialize Digipot Motor Current
  1095. {
  1096. EEPROM_read_st(EEPROM_SILENT,(uint8_t*)&SilentMode,sizeof(SilentMode));
  1097. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1098. if(SilentMode == 0){
  1099. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT_LOUD;
  1100. }else{
  1101. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1102. }
  1103. SPI.begin();
  1104. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1105. for(int i=0;i<=4;i++)
  1106. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1107. digipot_current(i,digipot_motor_current[i]);
  1108. #endif
  1109. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1110. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1111. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1112. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1113. if((SilentMode == 0) || (farm_mode) ){
  1114. motor_current_setting[0] = motor_current_setting_loud[0];
  1115. motor_current_setting[1] = motor_current_setting_loud[1];
  1116. motor_current_setting[2] = motor_current_setting_loud[2];
  1117. }else{
  1118. motor_current_setting[0] = motor_current_setting_silent[0];
  1119. motor_current_setting[1] = motor_current_setting_silent[1];
  1120. motor_current_setting[2] = motor_current_setting_silent[2];
  1121. }
  1122. digipot_current(0, motor_current_setting[0]);
  1123. digipot_current(1, motor_current_setting[1]);
  1124. digipot_current(2, motor_current_setting[2]);
  1125. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1126. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1127. #endif
  1128. }
  1129. void digipot_current(uint8_t driver, int current)
  1130. {
  1131. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1132. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1133. digitalPotWrite(digipot_ch[driver], current);
  1134. #endif
  1135. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1136. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1137. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1138. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1139. #endif
  1140. }
  1141. void microstep_init()
  1142. {
  1143. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1144. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1145. pinMode(E1_MS1_PIN,OUTPUT);
  1146. pinMode(E1_MS2_PIN,OUTPUT);
  1147. #endif
  1148. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1149. pinMode(X_MS1_PIN,OUTPUT);
  1150. pinMode(X_MS2_PIN,OUTPUT);
  1151. pinMode(Y_MS1_PIN,OUTPUT);
  1152. pinMode(Y_MS2_PIN,OUTPUT);
  1153. pinMode(Z_MS1_PIN,OUTPUT);
  1154. pinMode(Z_MS2_PIN,OUTPUT);
  1155. pinMode(E0_MS1_PIN,OUTPUT);
  1156. pinMode(E0_MS2_PIN,OUTPUT);
  1157. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1158. #endif
  1159. }
  1160. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1161. {
  1162. if(ms1 > -1) switch(driver)
  1163. {
  1164. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1165. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1166. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1167. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1168. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1169. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1170. #endif
  1171. }
  1172. if(ms2 > -1) switch(driver)
  1173. {
  1174. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1175. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1176. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1177. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1178. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1179. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1180. #endif
  1181. }
  1182. }
  1183. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1184. {
  1185. switch(stepping_mode)
  1186. {
  1187. case 1: microstep_ms(driver,MICROSTEP1); break;
  1188. case 2: microstep_ms(driver,MICROSTEP2); break;
  1189. case 4: microstep_ms(driver,MICROSTEP4); break;
  1190. case 8: microstep_ms(driver,MICROSTEP8); break;
  1191. case 16: microstep_ms(driver,MICROSTEP16); break;
  1192. }
  1193. }
  1194. void microstep_readings()
  1195. {
  1196. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1197. SERIAL_PROTOCOLPGM("X: ");
  1198. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1199. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1200. SERIAL_PROTOCOLPGM("Y: ");
  1201. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1202. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1203. SERIAL_PROTOCOLPGM("Z: ");
  1204. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1205. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1206. SERIAL_PROTOCOLPGM("E0: ");
  1207. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1208. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1209. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1210. SERIAL_PROTOCOLPGM("E1: ");
  1211. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1212. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1213. #endif
  1214. }
  1215. static void check_fans() {
  1216. if (READ(TACH_0) != fan_state[0]) {
  1217. fan_edge_counter[0] ++;
  1218. fan_state[0] = READ(TACH_0);
  1219. }
  1220. if (READ(TACH_1) != fan_state[1]) {
  1221. fan_edge_counter[1] ++;
  1222. fan_state[1] = READ(TACH_1);
  1223. }
  1224. }