stepper.cpp 41 KB

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